繁体   English   中英

不更改Datagrid中选定的颜色

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM