0

How can I highlight a cell in DataGrid that has been edited? XAML solution through some style using triggers is preferable. But if not possible, then a code behind approach would be good enough.

I am not posting any code as I did not have any break through with the problem.

4

1 回答 1

0

答案是简单地创建一个针对 DataGridCell 的样式并通过绑定触发条件,希望以下步骤易于遵循:

//lets say you bind datagrid to 
List<RowValues> RowsView {get;}
//were RowValues is
List<RowValue> RowValues
// and RowValue is 
public class RowValue
{
   public bool IsEdited 
   {
      get {return _isEdited;}
      set
      {
         if(_isEdited== value) return;
         _isEdited= value;
          RaisePropertyChanged(()=>IsEdited);
      }
   }
   public string Value 
   { 
      get {return _value;}
      set
      {
         if(_value == value) return;
         _value = value;
       //check if the value is edited
       IsEdited = _value == _originalValue;
       RaisePropertyChanged(()=>Value);
      }
   }

}

//so in code accessing the structure would look like:
var row = RowView[0];
var cell = row[1];
 cell.IsEdited... just to make it easier to see the XAML bindings below..


<DataGrid ItemsSource="{Binding RowsView}">
    <DataGridTemplateColumn>
        <DataGridTemplateColumn.CellStyle>
          <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Background" Value="Transparent" />
          <Style.Triggers>
          <DataTrigger Binding="{Binding RowValues[0].IsEdited}" Value="True">
             <Setter Property="Background" Value="{StaticResource MissingDataBrush}"/>
           </DataTrigger>
      </Style.Triggers>
    </Style>
</DataGridTemplateColumn.CellStyle>
<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding RowValues[0].Value}"/>
    </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
            <TextBox Text="{Binding RowValues[0].Value}"/>
        </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

……

于 2012-10-17T23:23:44.420 回答