相对于WPF数据网格中的前一个单元格值更改datagrid单元格背景色

时间:2012-10-10 06:35:12

标签: wpf user-controls wpf-controls wpfdatagrid wpftoolkit

我在WPF中创建了一个数据网格,它的单元格值不断变化,但是我无法根据单元格的前一个值更改单元格的背景颜色, 例如,如果单元格的值从10变为20,则单元格的颜色应变为绿色,表示该值增加,如果该值变为5,则单元格背景颜色应变为红色,表示该值已减小。我在datagridview的cellvaluechange事件的winforms中完成了这个,但在WPF中我无法做同样的事情。任何专业人士都对此有所帮助。

1 个答案:

答案 0 :(得分:1)

在您的单元格IsIncreased中创建一个属性DataContext,以根据增加/减少来保留Nullable<bool>

public class DatagridCellViewModel: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void FirePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool _isIncreased
    public bool? IsIncreased
    {
      get{ return _isIncreased; }
      set{ 
          _isIncreased = value, 
          FirePropertyChanged("IsIncreased");
         }
    }

    //this is going to store the value you have in your table. Don't forget to bind it to the cells content
    private int _dataValue;
    public int DataValue
    {
      get{ return _dataValue; }
      set{
            if(_dataValue != value)
            {
               IsIncreased = _dataValue < value
               _dataValue= value;

               FirePropertyChanged("DataValue");
            }             
         }
    }

    //other properties and methods
}

public class DataGridViewModel
{

     private ICollection<DataGridRowViewModel> _myDgRows = new ObservableCollection<DataGridRowViewModel>();
     public ObservableCollection<DataGridRowViewModel> MyDgRows { get{ return _myDgRows;}
}

public class DataGridRowViewModel : INotifyPropertyChanged // don't forget to implement this interface
{
     //put here fields and properties you want to display in a row in your datagrid
     //that means as many fields as many columns you have

    private DataGridCellViewModel _cellValue;
    public int CellValue
    {
      get{ return _cellValue; }
      set{
            if(!_cellValue.Equals(value))
            {
               _cellValue= value;

               FirePropertyChanged("CellValue");
            }             
         }
    }
}

创建DataTrigger以根据IsIncreased值设置单元格的背景:

<DataGrid ItemsSource="{Binding MyDgRows}" AutoGenerateColumns="False" >
<DataGrid.Columns>
    <DataGridTemplateColumn Header="ChangingValues" Width="SizeToCells">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Name="ContentTbx" Text="{Binding CellValue.DataValue}" />
               <DataTemplate.Triggers>
                  <DataTrigger Binding="{Binding CellValue.IsIncreased}" Value="True">
                     <Setter Property="Background" Value="Green" />
                  </DataTrigger>
                 <DataTrigger Binding="{Binding CellValue.IsIncreased}" Value="False">
                     <Setter TargetName="ContentTbx" Property="Background" Value="Red"/>
                 </DataTrigger>
               </DataTemplate.Triggers>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>

每次Data cells prop更改时设置IsIncreased prop

编辑:不要绑定表,但ObservableCollecition调用MyDgRows。为此,DataGridViewModel实例应该是您的DataGrids DataContext。

可能你需要在DataValue prop中处理int-string转换。

就此而言我用Google搜索了

  

datagrid wpf

第一个链接是this

  

datatriggers datatemplate

第二个结果是this

它们几乎涵盖了整个问题。 更自给自足