【问题标题】:How to highlight edited cell in a datagrid如何在数据网格中突出显示已编辑的单元格
【发布时间】:2012-07-10 10:19:59
【问题描述】:

如何突出显示 DataGrid 中已编辑的单元格?通过使用触发器的某种风格的 XAML 解决方案是可取的。但如果不可能,那么代码隐藏方法就足够了。

我没有发布任何代码,因为我没有解决问题。

【问题讨论】:

    标签: datagrid cell wpf-4.0


    【解决方案1】:

    答案是简单地创建一个以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>
    

    ....

    【讨论】:

      猜你喜欢
      • 2020-06-09
      • 2019-02-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多