Wpf 基于DataGridCell值的触发器
我在datagrid中有一些单元格,当它们的值为0时,我想用红色突出显示某些列中的单元格。我不知道该怎么做 我看过这个问题:但没有一个解决方案对我有效 使用样式触发器时,触发器似乎要应用于属性。当我做了一些类似于什么都没发生的事情时(我假设是因为内容不仅仅是一个简单的值) 对于上一个建议的解决方案,我遇到了一个编译时问题,这似乎是VS中一个bug的表现: 有什么办法可以做到这一点吗Wpf 基于DataGridCell值的触发器,wpf,binding,datagridcell,Wpf,Binding,Datagridcell,我在datagrid中有一些单元格,当它们的值为0时,我想用红色突出显示某些列中的单元格。我不知道该怎么做 我看过这个问题:但没有一个解决方案对我有效 使用样式触发器时,触发器似乎要应用于属性。当我做了一些类似于什么都没发生的事情时(我假设是因为内容不仅仅是一个简单的值) 对于上一个建议的解决方案,我遇到了一个编译时问题,这似乎是VS中一个bug的表现: 有什么办法可以做到这一点吗 有人有什么想法吗?根据DataGridCell的值更改单元格背景颜色的最佳方法是使用转换器为DataGridTem
有人有什么想法吗?根据DataGridCell的值更改单元格背景颜色的最佳方法是使用转换器为DataGridTemplateColumn定义DataTemplate以更改单元格的背景颜色。这里提供的示例使用MVVM 以下示例中要搜索的关键部件包括: 1:将模型中的整数(因子)转换为颜色的XAML:
<TextBlock Text="{Binding Path=FirstName}"
Background="{Binding Path=Factor,
Converter={StaticResource objectConvter}}" />
3:ViewModel,通过单击按钮在模型中更改0和1之间的整数值,以触发更改转换器中颜色的事件
private void OnChangeFactor(object obj)
{
foreach (var customer in Customers)
{
if ( customer.Factor != 0 )
{
customer.Factor = 0;
}
else
{
customer.Factor = 1;
}
}
}
4:模型实现INotifyPropertyChanged,用于通过调用OnPropertyChanged触发事件以改变背景颜色
private int _factor = 0;
public int Factor
{
get { return _factor; }
set
{
_factor = value;
OnPropertyChanged("Factor");
}
}
我已经在我的回答中提供了所有需要的位,除了用作
MVVM模式的基础,包括ViewModelBase(NoTiFyPryTyTrimeC改)和委派代码,您可以在谷歌中找到。注意,我将视图的DataContext绑定到代码隐藏的构造函数中的ViewModel。如果需要的话,我可以发布这些额外的信息
以下是XAML:
<Window x:Class="DatagridCellsChangeColor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Helpers="clr-namespace:DatagridCellsChangeColor.Converter"
Title="MainWindow"
Height="350" Width="525">
<Window.Resources>
<Helpers:ObjectToBackgroundConverter x:Key="objectConvter"/>
/Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="Change Factor" Command="{Binding Path=ChangeFactor}"/>
<DataGrid
Grid.Row="1"
Grid.Column="0"
Background="Transparent"
ItemsSource="{Binding Customers}"
IsReadOnly="True"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn
Header="First Name"
Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FirstName}"
Background="{Binding Path=Factor,
Converter={StaticResource objectConvter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn
Header="Last Name"
Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=LastName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
以下是ViewModel:
using System.Collections.ObjectModel;
using System.Windows.Input;
using DatagridCellsChangeColor.Commands;
using DatagridCellsChangeColor.Model;
namespace DatagridCellsChangeColor.ViewModel
{
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
ChangeFactor = new DelegateCommand<object>(OnChangeFactor, CanChangeFactor);
}
private ObservableCollection<Customer> _customers = Customer.GetSampleCustomerList();
public ObservableCollection<Customer> Customers
{
get
{
return _customers;
}
}
public ICommand ChangeFactor { get; set; }
private void OnChangeFactor(object obj)
{
foreach (var customer in Customers)
{
if ( customer.Factor != 0 )
{
customer.Factor = 0;
}
else
{
customer.Factor = 1;
}
}
}
private bool CanChangeFactor(object obj)
{
return true;
}
}
}
使用System.Collections.ObjectModel;
使用System.Windows.Input;
使用DatagridCellsChangeColor.Commands;
使用DatagridCellsChangeColor.Model;
命名空间DatagridCellsChangeColor.ViewModel
{
公共类MainViewModel:ViewModelBase
{
公共主视图模型()
{
ChangeFactor=新的DelegateCommand(OnChangeFactor、CanChangeFactor);
}
私有ObservableCollection_customers=Customer.GetSampleCustomerList();
公开收集客户
{
得到
{
退回客户;
}
}
公共ICommand变更因子{get;set;}
私有void OnChangeFactor(对象对象对象)
{
foreach(客户中的var客户)
{
如果(客户系数!=0)
{
顾客因素=0;
}
其他的
{
顾客因素=1;
}
}
}
私有布尔CanChangeFactor(对象对象对象)
{
返回true;
}
}
}
模型如下:
using System;
using System.Collections.ObjectModel;
using DatagridCellsChangeColor.ViewModel;
namespace DatagridCellsChangeColor.Model
{
public class Customer : ViewModelBase
{
public Customer(String first, string middle, String last, int factor)
{
this.FirstName = first;
this.MiddleName = last;
this.LastName = last;
this.Factor = factor;
}
public String FirstName { get; set; }
public String MiddleName { get; set; }
public String LastName { get; set; }
private int _factor = 0;
public int Factor
{
get { return _factor; }
set
{
_factor = value;
OnPropertyChanged("Factor");
}
}
public static ObservableCollection<Customer> GetSampleCustomerList()
{
return new ObservableCollection<Customer>(new Customer[4]
{
new Customer("Larry", "A", "Zero", 0),
new Customer("Bob", "B", "One", 1),
new Customer("Jenny", "C", "Two", 0),
new Customer("Lucy", "D", "THree", 2)
});
}
}
}
使用系统;
使用System.Collections.ObjectModel;
使用DatagridCellsChangeColor.ViewModel;
名称空间DatagridCellsChangeColor.Model
{
公共类客户:ViewModelBase
{
公共客户(字符串第一、字符串中间、字符串最后、整数因子)
{
this.FirstName=first;
this.MiddleName=last;
this.LastName=last;
这个系数=系数;
}
公共字符串名{get;set;}
公共字符串MiddleName{get;set;}
公共字符串LastName{get;set;}
私有整数系数=0;
公共整数因子
{
获取{return\u factor;}
设置
{
_系数=价值;
不动产变更(“因子”);
}
}
公共静态ObservableCollection GetSampleCustomerList()
{
返回新的ObservableCollection(新客户[4]
{
新客户(“拉里”,“A”,“零”,0),
新客户(“鲍勃”、“B”、“一”、1),
新客户(“珍妮”、“C”、“两个”、0),
新客户(“露西”,“D”,“三”,2)
});
}
}
}
根据DataGridCell的值更改单元格背景颜色的最佳方法是使用转换器为DataGridTemplateColumn定义DataTemplate以更改单元格的背景颜色。这里提供的示例使用MVVM
以下示例中要搜索的关键部件包括:
1:将模型中的整数(因子)转换为颜色的XAML:
<TextBlock Text="{Binding Path=FirstName}"
Background="{Binding Path=Factor,
Converter={StaticResource objectConvter}}" />
3:ViewModel,通过单击按钮在模型中更改0和1之间的整数值,以触发更改转换器中颜色的事件
private void OnChangeFactor(object obj)
{
foreach (var customer in Customers)
{
if ( customer.Factor != 0 )
{
customer.Factor = 0;
}
else
{
customer.Factor = 1;
}
}
}
4:模型实现INotifyPropertyChanged,用于通过调用OnPropertyChanged触发事件以改变背景颜色
private int _factor = 0;
public int Factor
{
get { return _factor; }
set
{
_factor = value;
OnPropertyChanged("Factor");
}
}
我已经在我的回答中提供了所有需要的位,除了用作
MVVM模式的基础,包括ViewModelBase(NoTiFyPryTyTrimeC改)和委派代码,您可以在谷歌中找到。注意,我将视图的DataContext绑定到代码隐藏的构造函数中的ViewModel。如果需要的话,我可以发布这些额外的信息
以下是XAML:
<Window x:Class="DatagridCellsChangeColor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Helpers="clr-namespace:DatagridCellsChangeColor.Converter"
Title="MainWindow"
Height="350" Width="525">
<Window.Resources>
<Helpers:ObjectToBackgroundConverter x:Key="objectConvter"/>
/Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="Change Factor" Command="{Binding Path=ChangeFactor}"/>
<DataGrid
Grid.Row="1"
Grid.Column="0"
Background="Transparent"
ItemsSource="{Binding Customers}"
IsReadOnly="True"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn
Header="First Name"
Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FirstName}"
Background="{Binding Path=Factor,
Converter={StaticResource objectConvter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn
Header="Last Name"
Width="SizeToHeader">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=LastName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
以下是ViewModel:
using System.Collections.ObjectModel;
using System.Windows.Input;
using DatagridCellsChangeColor.Commands;
using DatagridCellsChangeColor.Model;
namespace DatagridCellsChangeColor.ViewModel
{
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
ChangeFactor = new DelegateCommand<object>(OnChangeFactor, CanChangeFactor);
}
private ObservableCollection<Customer> _customers = Customer.GetSampleCustomerList();
public ObservableCollection<Customer> Customers
{
get
{
return _customers;
}
}
public ICommand ChangeFactor { get; set; }
private void OnChangeFactor(object obj)
{
foreach (var customer in Customers)
{
if ( customer.Factor != 0 )
{
customer.Factor = 0;
}
else
{
customer.Factor = 1;
}
}
}
private bool CanChangeFactor(object obj)
{
return true;
}
}
}
使用System.Collections.ObjectModel;
使用System.Windows.Input;
使用DatagridCellsChangeColor.Commands;
使用DatagridCellsChangeColor.Model;
命名空间DatagridCellsChangeColor.ViewModel
{
公共类MainViewModel:ViewModelBase
{
公共主视图模型()
{
ChangeFactor=新的DelegateCommand(OnChangeFactor、CanChangeFactor);
}
私有ObservableCollection_customers=Customer.GetSampleCustomerList();
公开收集客户
{
得到
{
退回客户;
}
}
公共ICommand变更因子{get;set;}
私有void OnChangeFactor(对象对象对象)
{
foreach(客户中的var客户)
{
如果(客户系数!=0)
{
顾客因素=0;
}
其他的
{
顾客因素=1;
}
}
}