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

u4vypkhs  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(178)

我正在努力让datagridrow的颜色根据行“IsSelected”属性更改颜色。我看了所有的例子,并尝试了所有我能做的最好的,但仍然不能让它工作。嗯,实际上它在应用程序的初始启动时工作,但当我更改datagrid的“SelectedIndex”时,选定的索引行将高亮显示,就像它被选中一样,但颜色将恢复为默认值,不会更改。
用户无法与此网格交互。所有操作都在代码隐藏中完成,包括它所具有的行数以及DataGrid中的哪一行“IsSelected”。我在代码隐藏中将Datagrid的“SelectedIndex”属性绑定到一个属性值。如前所述,设置和更改该属性可以正常工作,并且在更改“SelectedIndex”属性时DataGrid的行可以正常突出显示。但行的背景色和前景色没有相应地更改。
我已经为Datagrid的行创建了一个行样式,如下所示:

<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>

“SelectedIndex”属性由另一个外部应用程序设置。它工作正常。这就是我如何更改IsSelected的行。
将“SelectedIndex”设置为“1”,然后初始启动应用程序,它工作正常,下面是网格的外观,这正是我所需要的。

现在,打开应用程序,然后将DataGrid的“SelectedIndex”更改为“2”,我希望网格中的第三行将颜色更改为红色,但得到的却是:

它很好地突出显示了行索引,但是颜色没有像我预期的那样发生变化。
下面是定义DataGrid的XAML。我在应用程序启动时设置行和列的大小以及代码隐藏中的数据(数据表)。

<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>

我很感激任何指导。谢谢。

d6kp6zgx

d6kp6zgx1#

不仅设置数据网格行,还设置数据网格单元格

<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>

相关问题