[英]How to Update DataGrid Cell Background when the Cell Value is Changed
All, I have the following XAML to change the cells background colour at run-time based on whether the cells content is 'Help'. 所有,我都有以下XAML可以在运行时根据单元格内容是否为“帮助”来更改单元格背景色。
<UserControl.Resources>
<local:CellColorConverter x:Key ="cellColorConverter"/>
</UserControl.Resources>
<DataGrid x:Name="dataGrid" AlternatingRowBackground="Gainsboro"
AlternationCount="2" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell" >
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource cellColorConverter}" >
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}"/>
<Binding Path="Row"/>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FF007ACC"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
The CellColorConverter
class handle the 'converion'/colour update. CellColorConverter
类处理“转换” /颜色更新。
public class CellColorConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[1] is DataRow)
{
//Change the background of any cell with 1.0 to light red.
var cell = (DataGridCell)values[0];
var row = (DataRow)values[1];
var columnName = cell.Column.SortMemberPath;
if (row[columnName].ToString().CompareTo("Help") == 0)
return new SolidColorBrush(Colors.LightSalmon);
}
return SystemColors.AppWorkspaceColor;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
This works when the data is loaded in, but I also want the colour to be updated if the user types 'Help' into a cell. 这在加载数据时有效,但是如果用户在单元格中键入“帮助”,我还希望更新颜色。 So I have tried amending the binding to 所以我尝试修改绑定到
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource cellColorConverter}" >
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}" // Changed this!
Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="Row"/>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
But this has not worked. 但这没有用。 How to I get the background cell colour to change when the cell value is changed and committed? 如何更改和提交单元格值时更改背景单元格颜色?
Thanks for your time. 谢谢你的时间。
If I can assume your data grid is bound to a collection of items in a view model, then for a specific column you can use a DataTrigger: 如果我可以假设您的数据网格绑定到视图模型中的项目集合,那么对于特定列,您可以使用DataTrigger:
<DataGrid ItemsSource="{Binding Items}" ...>
<DataGrid.Columns>
... columns
<DataGridTextColumn Header="My column"
Binding="{Binding MyItem, UpdateSourceTrigger=PropertyChanged}">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<DataTrigger Binding="{Binding MyItem}" Value="Help">
<Setter Property="Background" Value="LightSalmon"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
The problem would be if you needed to apply this to all columns, in which case you would need a separate style for each. 问题将出在是否需要将其应用于所有列,在这种情况下,您将需要为每个列使用单独的样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.