gpt4 book ai didi

WPF Datagrid 行颜色基于值

转载 作者:行者123 更新时间:2023-12-04 21:44:39 24 4
gpt4 key购买 nike

我想根据 bool 值更改 DataGrid 的行颜色。我看过 https://stackoverflow.com/questions/18580612/wpf-datagrid-trigger-row-colour-based-on-value并以类似的方式尝试它(在本地定义样式而不是在用户控件的资源中定义它),但是当 editable 属性更改时,行背景不会更改。我不知道出了什么问题,很乐意尝试一些新事物。

<DataGrid ItemsSource="{Binding ReportSampleExaminationList}" CanUserDeleteRows="False" PreviewKeyDown="deleteRow" AutoGenerateColumns="False" HorizontalAlignment="Stretch" 
RowBackground="Wheat" AlternatingRowBackground="WhiteSmoke" VerticalGridLinesBrush="Transparent" HorizontalGridLinesBrush="Gray">

<DataGrid.CellStyle >
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter Property="Background"
Value="LightBlue" />
<Setter Property="Foreground"
Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Editable}" Value="False">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>

<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Editable" Binding="{Binding Editable}"/>
<DataGridTextColumn Header="Probe" Binding="{Binding SampleNumber}" IsReadOnly="True" />
</DataGrid.Columns>

</DataGrid>

最佳答案

如果您从 DataGrid 中取出 RowBackground 和 AlternatingRowBackground,它就可以工作。如果您想使用它们,您必须将它们放入 DataGrid 的样式中。

<DataGrid ItemsSource="{Binding ReportSampleExaminationList}" CanUserDeleteRows="False" PreviewKeyDown="deleteRow" AutoGenerateColumns="False" HorizontalAlignment="Stretch" 
VerticalGridLinesBrush="Transparent" HorizontalGridLinesBrush="Gray">
<DataGrid.Style>
<Style TargetType="DataGrid">
<Setter Property="RowBackground" Value="Wheat" />
<Setter Property="AlternatingRowBackground" Value="WhiteSmoke" />
</Style>
</DataGrid.Style>
<DataGrid.CellStyle >
<Style TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="LightBlue" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Editable}" Value="False">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>

<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Editable" Binding="{Binding Editable,UpdateSourceTrigger=PropertyChanged}"/>
<DataGridTextColumn Header="Probe" Binding="{Binding SampleNumber}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>

关于WPF Datagrid 行颜色基于值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35016699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com