1

我在 WPF 中创建了一个数据网格,它的单元格值不断变化,但我无法更改单元格相对于单元格先前值的背景颜色,例如,如果单元格的值从 10 变为 20单元格的颜色应变为绿色,表示值增加,如果值变为 5,则单元格背景颜色应变为红色,表示值已减小。我已经在 datagridview 的 cellvaluechange 事件的 winforms 中做到了这一点,但在 WPF 中我无法做到这一点。任何专业人士都为此做过任何事情。

4

1 回答 1

1

IsIncreased在您的单元格中创建一个属性以根据增加/减少DataContext来保存 a 。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>

每次单元格数据道具更改时设置 IsIncreased 道具

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

可能您需要在 DataValue 道具中处理 int-string 转换。

为此,我用谷歌搜索

数据网格 wpf

第一个链接是这个

数据触发数据模板

第二个结果是这个

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

于 2012-10-10T06:55:20.997 回答