【发布时间】:2010-12-24 22:14:26
【问题描述】:
当我从代码(通过 ViewModel 中的绑定对象)更新数据网格 SelectedItem 时,如何让可视网格突出显示新选择的项目?
谢谢,
标记
更新:这对我来说仍然是个问题。我的 SelectedItem 属性已经实现了更改通知,但数据网格没有视觉上显示已选择的行 - 即它没有被突出显示。
【问题讨论】:
标签: silverlight-3.0 datagrid highlighting selecteditem
当我从代码(通过 ViewModel 中的绑定对象)更新数据网格 SelectedItem 时,如何让可视网格突出显示新选择的项目?
谢谢,
标记
更新:这对我来说仍然是个问题。我的 SelectedItem 属性已经实现了更改通知,但数据网格没有视觉上显示已选择的行 - 即它没有被突出显示。
【问题讨论】:
标签: silverlight-3.0 datagrid highlighting selecteditem
我猜你真的验证了SelectedItem 已经改变(你可以暂时将Binding 模式设置为TwoWay 以查看它是否可以通过单击行,您应该会看到高亮显示并执行了 SelectedItem 的 set-方法)。如果是,请验证您真的与PropertyChanged 方法调用上的属性名称完全匹配。由于您在这里不是类型安全的,因此您可能犯了拼写错误。如果不是,请检查您的数据绑定属性是否设置正确。另一个想法是您可能已经更改了DataGrid 的样式或模板,现在您缺少一些视觉状态。 DataGridRow 可以使用样式模板设置样式。您有三个选定的状态,称为UnfocusedSelected(可能是正确的)、NormalSelected 和MouseOverSelected。
您可以尝试使用此模板制作自己的视觉状态:
<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>
这是来自 MSDN Article 的关于自定义 DataGrid 样式的复制粘贴。例如,您可以修改模板的 UnfocusedSelected 部分,然后查看是否有任何变化,例如在其周围添加红色边框或其他内容。
也许值得一试。我希望你知道如何应用自己的风格。如果没有,这里是另一个MSDN Resource。
我知道,这些只是提示,但最终可能会有所帮助。
【讨论】:
您需要在 ViewModel 上实现 INotifyPropertyChanged 接口,并让其 SelectedItem 属性在其值发生更改时调用 PropertyChanged 事件。
【讨论】: