[英]Does not change color selected row in Datagrid
I want to load window in Datagrid 5 row selected 我想在选定的Datagrid 5行中加载窗口
My code, but color does not change 我的代码,但颜色不变
DataGridRow row = (DataGridRow)DataGridService.ItemContainerGenerator.ContainerFromIndex(5);
object item = DataGridService.Items[5];
DataGridService.SelectedItem = item;
DataGridService.ScrollIntoView(item);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
Xaml XAML
<DataGrid.Resources>
<SolidColorBrush x:Key="SelectionColorKey" Color="Red"/>
<Style TargetType="DataGridRow">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding Source={StaticResource SelectionColorKey}, Path=Color}"/>
</Style.Resources>
</Style>
</DataGrid.Resources>
May be i not correct formulated problem. 可能是我不正确制定问题。 I need on load window, background row 5 was red color and focus was in 5 row
我需要在加载窗口上,背景第5行为红色,焦点位于5行
It does not work 这是行不通的
DataGridService.SelectedItem = DataGridService.Items[5];
strangely in Winforms this makes easy 奇怪的是,在Winforms中,这很容易
How selected first row? 如何选择第一行?
Why is the problem with selected color? 为什么选择的颜色有问题?
You're defining a style but don't do anything in this style. 您正在定义一种样式,但是什么也不做。 You have to define some
Setter
s to set properties of the styled object. 您必须定义一些
Setter
来设置样式对象的属性。
Something like this: 像这样:
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
I found the solution, it is necessary to set the focus on Datagrid 我找到了解决方案,有必要将重点放在Datagrid上
DataGridService.Focus();
DataGridRow row = (DataGridRow)DataGridService.ItemContainerGenerator.ContainerFromIndex(100);
object item = DataGridService.Items[100];
DataGridService.SelectedItem = item;
DataGridService.ScrollIntoView(item);
row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.