当我从代码更新数据网格SelectedItem时(通过viewmodel中的绑定对象),如何让可视网格突出显示新选择的项目?
谢谢,
标记
更新:这对我来说仍然是一个问题.我的SelectedItem属性已经实现了更改通知,但是数据网格没有显示选择了哪一行 – 即它没有被突出显示.
解决方法
我猜你确实已经验证了SelectedItem已经改变了(你可以暂时将Binding Mode设置为TwoWay以查看它是否相反,通过单击你应该看到突出显示的行和SelectedItem的set-method执行).如果是,请验证您是否与PropertyChanged方法调用上的属性名称完全匹配.由于您在此处不是类型安全的,因此可能会出现拼写错误.如果不是,请检查您的数据绑定属性是否设置正确.另一个想法是,您可能已经更改了DataGrid的样式或模板,现在您缺少一些Visual状态.可以使用样式模板设置DataGridRow的样式.您有三个选定状态,称为UnfocusedSelected(可能是右侧),NormalSelected和MouSEOverSelected.
您可以尝试使用此模板创建自己的Visual State:
<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>
这是一个关于自定义DataGrid样式的好的MSDN Article的复制粘贴.例如,您可以修改模板的UnfocusedSelected部分,看看是否看到任何更改,例如,在其周围添加红色边框等.
也许这值得一试.我希望你知道如何应用自己的风格.如果没有,这是另一个MSDN Resource.
我知道,这些只是提示,但最后可能会有所帮助.