简体   繁体   English

如何根据“IsSelected”属性更改 datagridrow 背景颜色?

[英]How to change datagridrow backcolor based on "IsSelected" property?

I'm struggling to get the colors of a datagridrow to change colors based on row "IsSelected" property.我正在努力获取数据网格的 colors 以根据行“IsSelected”属性更改 colors。 I have looked at all examples and tried all the best I can but still can't get it to work.我查看了所有示例并尽我所能,但仍然无法使其正常工作。 Well, actually it works on initial startup of the application, but when I change the datagrid's "SelectedIndex", the selected index row get's highlighted as if it's selected but the colors revert to default and don't change.好吧,实际上它适用于应用程序的初始启动,但是当我更改数据网格的“SelectedIndex”时,选定的索引行会突出显示,好像它已被选中,但 colors 恢复为默认值并且不更改。

The user cannot interact with this grid.用户无法与此网格交互。 Everything is done in code-behind, including the number of rows it has and which row "IsSelected" in the DataGrid.一切都在代码隐藏中完成,包括它拥有的行数以及 DataGrid 中的哪一行“IsSelected”。 I have the "SelectedIndex" property of the Datagrid bound to a property value in code behind.我将 Datagrid 的“SelectedIndex”属性绑定到后面代码中的属性值。 As noted, setting and changing that property works fine and the row of the DataGrid highlights fine as I change the "SelectedIndex" property.如前所述,设置和更改该属性可以正常工作,并且当我更改“SelectedIndex”属性时,DataGrid 的行突出显示正常。 But the background and foreground colors of the row don't change appropriately.但是行的背景和前景 colors 没有适当改变。

I have created a rowstyle for the Datagrid's rows as follows:我为 Datagrid 的行创建了一个 rowstyle,如下所示:

 <Style x:Key="RowStyle3" TargetType= "{x:Type DataGridRow}">
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Bottom"/>
    <Setter Property="IsHitTestVisible" Value="False"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
        <Trigger Property="IsSelected" Value="False">
            <Setter Property="Background" Value="#FF232E3C"/>
            <Setter Property="Foreground" Value="#FFC8E5FF"/>
        </Trigger>
    </Style.Triggers>
</Style>

The "SelectedIndex" property gets set by another external application. “SelectedIndex”属性由另一个外部应用程序设置。 It works fine.它工作正常。 That is how I am changing which row IsSelected.这就是我更改哪一行 IsSelected 的方式。

With "SelectedIndex" set to '1', and then initial start of the application , it works fine and here is what the grid looks like, which is what I need.将“SelectedIndex”设置为“1”,然后初始启动应用程序,它工作正常,这就是网格的样子,这就是我需要的。 应用程序初始启动时的 DataGrid。选定索引 = 1

Now, with the application open and then change the "SelectedIndex" of the DataGrid to '2', I expect the the third row in the grid to change color to red, but this is what I get instead:现在,打开应用程序,然后将 DataGrid 的“SelectedIndex”更改为“2”,我希望网格中的第三行将颜色更改为红色,但这是我得到的: 在此处输入图像描述

It highlights the row index fine, but the colors didn't change like I expect.它很好地突出了行索引,但 colors 并没有像我预期的那样发生变化。

Here is the XAML that defines the DataGrid.这是定义 DataGrid 的 XAML。 I'm setting the size of the rows and columns as well as the data (datatable) in code-behind when the app starts.我在应用程序启动时在代码隐藏中设置行和列的大小以及数据(数据表)。

<DataGrid x:Name="DG_TestRuntime"
          HorizontalAlignment="Left" Height="695.936" VerticalAlignment="Top" Width="1223.789"
          VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
          ColumnHeaderHeight="48" ColumnHeaderStyle="{DynamicResource DGCHeaderStyle}"
          CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False"
          SelectionMode="Single" CanUserResizeRows="False"
          HorizontalGridLinesBrush="#FF688CAF" VerticalGridLinesBrush="#FF688CAF"
          HeadersVisibility="Column" CanUserAddRows="False"
          HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Disabled"
          IsEnabled="True" Background="#FF232E3C" Foreground="#FFC8E5FF"
          SelectedIndex="{Binding StepNumber, Mode=OneWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}"
          RowStyle="{DynamicResource RowStyle3}">
    <DataGrid.DataContext>
        <local:MyProperties/>
    </DataGrid.DataContext>
</DataGrid>

I am grateful for any guidance.我很感激任何指导。 Thank you.谢谢你。

Set not only DataGridRow but also DataGridCell不仅要设置DataGridRow ,还要设置DataGridCell

<Style TargetType= "{x:Type DataGridRow}">
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Bottom"/>
    <Setter Property="IsHitTestVisible" Value="False"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
        <Trigger Property="IsSelected" Value="False">
            <Setter Property="Background" Value="#FF232E3C"/>
            <Setter Property="Foreground" Value="#FFC8E5FF"/>
        </Trigger>
    </Style.Triggers>
</Style>
<Style TargetType= "{x:Type DataGridCell}">
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Bottom"/>
    <Setter Property="IsHitTestVisible" Value="False"/>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="BorderThickness" Value="0"/>
        </Trigger>
        <Trigger Property="IsSelected" Value="False">
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Foreground" Value="#FFC8E5FF"/>
            <Setter Property="BorderThickness" Value="0"/>
        </Trigger>
    </Style.Triggers>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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