所以我试图根据Datagrid的值更改Datagrid中单元格的背景。因此,如果我想根据静态值更改它,我会使用StyleTrigger,但事实并非如此。
例如:如果单元格的值介于80和100之间,则需要绿色背景;如果值介于40和79之间,则需要橙色背景;如果低于40,则需要红色背景...我将如何实现?
目前我正在使用StyleTrigger进行测试,所以不要介意......
这是我的XAML代码:
<DataGrid Background="LightGray" ItemsSource="{Binding Source={StaticResource Properties}, Path=TableData}"
AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns >
<DataGridTextColumn Width="100" Header="ID" Binding="{Binding ID}"></DataGridTextColumn>
<DataGridTextColumn Width="100" Header="Batterie" Binding="{Binding Battery}" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Text" Value="83">
<Setter Property="Background" Value="LightGreen"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Width="100" Header="Current Use" Binding="{Binding CurrentUse}"></DataGridTextColumn>
<DataGridTextColumn Width="100" Header="Occupancy" Binding="{Binding Occupancy}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
任何帮助表示赞赏!!
答案 0 :(得分:1)
我通过编写一个从IValueConverter接口实现的Converter来解决这个问题。
如果你想在XAML中看到你的转换器,你需要将它引用到像这样的静态资源
<Window.Resources>
<local:myConverter x:Key="myConverter"/>
</Window.Resources>
我的设计;
<Grid>
<DataGrid Name="dgList" Background="LightGray" AutoGenerateColumns="False" IsReadOnly="True">
<DataGrid.Columns >
<DataGridTextColumn Width="100" Header="ID" Binding="{Binding ID}"></DataGridTextColumn>
<DataGridTextColumn Width="100" Header="Batterie" Binding="{Binding Battery}" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="{Binding Battery, Converter={StaticResource myConverter}}"></Setter>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Width="100" Header="Current Use" Binding="{Binding CurrentUse}"></DataGridTextColumn>
<DataGridTextColumn Width="100" Header="Occupancy" Binding="{Binding Occupancy}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
此绑定将您的数据电池(10,30,40,80等)发送到转换器。转换器返回一个对象,我们的对象必须是一个SolidColorBrush来绑定它为BackGround.That&# 39; s all。我解决了这个问题
<Setter Property="Background" Value="{Binding Battery, Converter={StaticResource myConverter}}"></Setter>
myConverter用于将数据转换为背景;
public class myConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush clr;
if (int.Parse(value.ToString()) >= 80)
clr = new SolidColorBrush(Colors.Green);
else if (int.Parse(value.ToString()) >= 40)
clr = new SolidColorBrush(Colors.Orange);
else if (int.Parse(value.ToString()) >= 0)
clr = new SolidColorBrush(Colors.Red);
else
clr = new SolidColorBrush(Colors.White);
return clr;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我为你的例子创建了一个课程(如果对不起,我没有看到你的那个)
public class Class1
{
public int ID { get; set; }
public string Battery { get; set; }
public string CurrentUse { get; set; }
public string Occupancy { get; set; }
public static List<Class1> myList = new List<Class1>()
{
new Class1() {ID = 1, Battery = "70", CurrentUse = "xxxx", Occupancy = "xxxx" },
new Class1() {ID = 2, Battery = "100", CurrentUse = "xxxx", Occupancy = "xxxx" },
new Class1() {ID = 3, Battery = "10", CurrentUse = "xxxx", Occupancy = "xxxx" },
new Class1() {ID = 4, Battery = "50", CurrentUse = "xxxx", Occupancy = "xxxx" },
new Class1() {ID = 5, Battery = "80", CurrentUse = "xxxx", Occupancy = "xxxx" },
new Class1() {ID = 6, Battery = "40", CurrentUse = "xxxx", Occupancy = "xxxx" },
new Class1() {ID = 7, Battery = "39", CurrentUse = "xxxx", Occupancy = "xxxx" }
};
}
在MainWindow上
private void Window_Loaded(object sender, RoutedEventArgs e)
{
dgList.ItemsSource = Class1.myList;
}
我的项目截图。