我一直在尝试很多不同的方法来更改从某一列返回“1”的行的单元格颜色。我的所有尝试都碰壁了。
这是我正在查看的内容 - 如果列有“1”,这是我的选择:
我的 xaml 为
JobId
如果值为 true。
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsHitTestVisible" Value="False"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Suspended}" Value="Running">
<Setter Property="Background" Value="#FFEA2B0D"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns >
<DataGridTextColumn Binding="{Binding Id}" Width="40" Header=" Job ID " Foreground="Black" IsReadOnly="True" >
<DataGridTextColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="#FF8A8A8A"/>
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
string lbcompjob = ("SELECT * FROM " + Table + " WHERE [cancled] = 1");
SqlCommand cmd = new SqlCommand(lbcompjob, conn);
{
try
{
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
string can = dr[0].ToString();
if (can == "1")
{
// If True change row color
}
}
}
}
catch (Exception ex)
{
}
}
只需添加一个触发器到
RowStyle
:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsHitTestVisible" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding Suspended}" Value="Running">
<Setter Property="Background" Value="#FFEA2B0D"/>
</DataTrigger>
<!-- Add your trigger here -->
<DataTrigger Binding="{Binding ID}" Value="1">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>