根据其值(C#,WPF)设置DataGrid单元格背景,可以通过自定义DataGrid的CellStyle来实现。具体步骤如下:
<DataGrid x:Name="myDataGrid" AutoGenerateColumns="False">
<!-- Define columns here -->
</DataGrid>
public class MyDataItem
{
public string Value { get; set; }
public MyDataItem(string value)
{
Value = value;
}
}
ObservableCollection<MyDataItem> dataItems = new ObservableCollection<MyDataItem>();
myDataGrid.ItemsSource = dataItems;
<DataGrid.Columns>
<DataGridTextColumn Header="Value" Binding="{Binding Value}">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="White"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Value}" Value="SomeValue">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding Value}" Value="AnotherValue">
<Setter Property="Background" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
在上述代码中,我们为DataGrid的Value列定义了一个CellStyle,并使用DataTrigger来根据Value的值设置单元格的背景颜色。当Value为"SomeValue"时,背景色为绿色;当Value为"AnotherValue"时,背景色为黄色。
dataItems.Add(new MyDataItem("SomeValue"));
dataItems.Add(new MyDataItem("AnotherValue"));
这样,当DataGrid绑定的数据项的Value属性为"SomeValue"或"AnotherValue"时,对应的单元格背景色将会根据设置的样式而改变。
推荐的腾讯云相关产品:腾讯云服务器(CVM)和腾讯云数据库(TencentDB)。腾讯云服务器提供了高性能、可扩展的云服务器实例,可满足各种规模的应用需求。腾讯云数据库提供了多种数据库引擎,包括关系型数据库(MySQL、SQL Server等)和非关系型数据库(MongoDB、Redis等),可满足不同类型的数据存储需求。
腾讯云服务器(CVM)产品介绍链接地址:https://cloud.tencent.com/product/cvm 腾讯云数据库(TencentDB)产品介绍链接地址:https://cloud.tencent.com/product/cdb
领取专属 10元无门槛券
手把手带您无忧上云