Silverlight DataGrid Updating SelectedItem from Code

When I update a selected data item from code (via a related object in the ViewModel), how do I get a visual grid to highlight a newly selected item?

Thanks,
Mark

UPDATE: This is still a problem for me. The My SelectedItem property already implements notification of changes, but the datagrid does not visually display which row was selected, i.e. Will not be highlighted.

+6
highlighting datagrid selecteditem
source share
2 answers

I assume that you really confirmed that the SelectedItem has changed (you can temporarily set the Binding mode to TwoWay to see if it works the other way around, by clicking on the line you should see the selection and the SelectedItem set method is done). If so, make sure that you really match the property name in the PropertyChanged invoke method. Since you are not typical here, you may have made a spelling mistake. If not, check if the data binding attribute is set correctly. Another idea is that you may have changed the styles or the DataGrid template, and now you are missing some visual states . DataGridRow can be styled using a style template. You have three selected states called UnfocusedSelected (possibly the correct one), NormalSelected and MouseOverSelected .

You can try to create your own visual state using this template:

 <Style TargetType="local:DataGridRow"> <Setter Property="IsTabStop" Value="False" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:DataGridRow"> <localprimitives:DataGridFrozenGrid Name="Root"> <vsm:VisualStateManager.VisualStateGroups> <vsm:VisualStateGroup x:Name="CommonStates"> <vsm:VisualState x:Name="Normal"/> <vsm:VisualState x:Name="NormalAlternatingRow"> <Storyboard> <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="0"/> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="MouseOver"> <Storyboard> <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To=".5"/> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="NormalSelected"> <Storyboard> <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="MouseOverSelected"> <Storyboard> <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> </Storyboard> </vsm:VisualState> <vsm:VisualState x:Name="UnfocusedSelected"> <Storyboard> <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Fill).Color" To="#FFE1E7EC"/> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> <vsm:VisualStateGroup x:Name="ValidationStates"> <vsm:VisualState x:Name="Valid"/> <vsm:VisualState x:Name="Invalid"> <Storyboard> <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Visibility"> <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/> </ObjectAnimationUsingKeyFrames> <DoubleAnimation Storyboard.TargetName="InvalidVisualElement" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/> </Storyboard> </vsm:VisualState> </vsm:VisualStateGroup> </vsm:VisualStateManager.VisualStateGroups> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.Resources> <Storyboard x:Key="DetailsVisibleTransition"> <DoubleAnimation Storyboard.TargetName="DetailsPresenter" Storyboard.TargetProperty="ContentHeight" Duration="00:00:0.1" /> </Storyboard> </Grid.Resources> <Rectangle x:Name="BackgroundRectangle" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="#FFBADDE9"/> <Rectangle x:Name="InvalidVisualElement" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="#FFF7D8DB"/> <localprimitives:DataGridRowHeader Grid.RowSpan="3" Name="RowHeader" localprimitives:DataGridFrozenGrid.IsFrozen="True" /> <localprimitives:DataGridCellsPresenter Grid.Column="1" Name="CellsPresenter" localprimitives:DataGridFrozenGrid.IsFrozen="True" /> <localprimitives:DataGridDetailsPresenter Grid.Row="1" Grid.Column="1" Name="DetailsPresenter" /> <Rectangle Grid.Row="2" Grid.Column="1" Name="BottomGridLine" HorizontalAlignment="Stretch" Height="1" /> </localprimitives:DataGridFrozenGrid> </ControlTemplate> </Setter.Value> </Setter> </Style> 

This is a copy with a good MSDN article on customizing DataGrid styles. For example, you can change part of the UnfocusedSelected template and see if you see any changes, for example, adding a red frame to it or something like that.

Maybe worth a try. I hope you know how to apply your own styles. If not, here is another MSDN resource .

I know these are just tips, but maybe useful, finally.

+3
source share

You need to implement the INotifyPropertyChanged interface on your ViewModel and have the SelectedItem property raise the PropertyChanged event when its value changes.

0
source share

All Articles