[英]Does not change color selected row in Datagrid
我想在選定的Datagrid 5行中加載窗口
我的代碼,但顏色不變
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
<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>
可能是我不正確制定問題。 我需要在加載窗口上,背景第5行為紅色,焦點位於5行
這是行不通的
DataGridService.SelectedItem = DataGridService.Items[5];
奇怪的是,在Winforms中,這很容易
如何選擇第一行?
為什么選擇的顏色有問題?
您正在定義一種樣式,但是什么也不做。 您必須定義一些Setter
來設置樣式對象的屬性。
像這樣:
<DataGrid.Resources>
<Style TargetType="DataGridRow">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
我找到了解決方案,有必要將重點放在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.