WPF DataGrid编辑后更新单元格样式 [英] WPF DataGrid updating cell style after editing

查看:467
本文介绍了WPF DataGrid编辑后更新单元格样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在XAML中,我在DataGrid中有一列,定义如下:

In XAML I have a column in a DataGrid that is defined like this:

<DataGridTextColumn Header="Name" Binding="{Binding Name}">
  <DataGridTextColumn.CellStyle>
    <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}" >
      <Setter Property="Background" Value="{Binding Converter={StaticResource NameToBrushConverter}}"/>
    </Style>
  </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

当名称列和名字列具有相同的内容时,NameToBrushConverter返回颜色。否则会返回DependencyProperty.UnsetValue。

The NameToBrushConverter returns a color when the "Name" column und the "FirstName" column have the same content. Otherwise it returns DependencyProperty.UnsetValue.

我遇到的问题是,如果我编辑单元格并结束编辑,则样式不会更新。只有当新输入的值移动到不同的行(由于排序)时,才会应用背景条件着色。但是如果在编辑之后,该对象将显示在数据网格的同一行中,那么背景颜色将不会被更新,直到我点击排序。只要单元格移动到不同的行,背景将根据转换器值进行更新。

The problem I'm facing is that if I edit a cell and end editing, the style is not updated. Only if the newly entered value moves to a different line (because of sorting) the conditional coloring of the background is applied. But if after editing the object is displayed in the same row of the datagrid the background color get's not updated until I click on sort. As soon as the cell moves to a different row the background will be updated according to the Converter value.

为对象实现INotifyPropertyChanged并没有帮助。

Implementing INotifyPropertyChanged for the object doesn't help.

有没有办法告诉GridView编辑单元格后必须重新评估样式?

Is there a way to tell the GridView that it has to reevaluate the styling after editing a cell?

dataGrid.Items.Refresh();

调用刷新有助于,但哪个是触发刷新的正确事件?我在CellEditEnding中尝试过但是有一个异常AddNew-或EditItem事务中不允许刷新。

Calling refresh helps, but which is the right event to trigger a refresh? I tried it in CellEditEnding but got an exception "Refresh is not allowed in AddNew- or EditItem transactions".

推荐答案

1)你需要将UpdateSourceTrigger设置为PropertyChanged,因为它默认设置为DataGrid中的LostFocus。

1) You need to set UpdateSourceTrigger to PropertyChanged because it is by default set to LostFocus within a DataGrid.

2)我猜你没有CellEditTemplate。

2) I'm guessing that you do not have a CellEditTemplate.

3)这是所有最大的问题。您必须使用相关转换器在属性上使用多字段绑定。这是现在工作的唯一原因是因为当发生丢失的焦点时,当前单元格中的绑定更新,并将您的项目(即绑定或绑定到Path =。)传递给转换器并输出一些颜色。

3) This is the biggest issue of all You would have to use multibinding on your properties with a relevant Converter. The only reason this is working now is because when lost focus occurs the binding in the current cell refresh and gets your item (i.e. binding or binding to Path=.) passing it to the converter and outputting some color.

编辑:

我知道我把UpdateSourceTrigger放在错误的绑定上。
将其放在上面的名称上,在你的单元格中也绑定到名称。

I know see that i put the UpdateSourceTrigger on the wrong binding. Place it on the Name above and in your cellstyle also bind to Name.

XAML:

 <DataGrid>
       <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged}">
                <DataGridTextColumn.CellStyle>
                       <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}" >
                      <Setter Property="Background" Value="{Binding Name ,Converter={StaticResource NameToBrushConverter}}"/>
                       </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
        </DataGrid.Columns>
   </DataGrid> 

此外,关于下面的问题。在行之间移动时,它部分工作的唯一原因是因为DataGrid中嵌套的每个绑定的默认UpdateSourceTrigger。这是LostFocus。

Further more, as for your question below. The only reason it was working in partially when moving between rows is because of the default UpdateSourceTrigger for every binding nested in a DataGrid. which is LostFocus.

当使用

  <SomeElement Tag={Binding} />
  Or 
  <SomeElement Tag={Binding Path=.} />        

您没有绑定到属性。

Binding在以下情况下进行评估:

The Binding is evaluated when :

1)DependencyObject初始化,所有的DP都被评估。它首次获得它的价值。

1) The DependencyObject initializes and all it's DP's are evaluated. It gets it's value for the first time.

2)UpdateSourceTrigger = LostFocus(DataGrid中的默认值)它在LostFocus上发生。这就是为什么当您在行之间传递时,您的Binding被评估。

2) UpdateSourceTrigger=LostFocus (The default inside the DataGrid) it is eventuated on LostFocus. This is why your Binding is evaluated when you pass between rows.

3)UpdateSourceTrigger = PropertyChanged。如果你不想在你的数据文本上,你必须明确地设置一个属性,它会返回自己,并在name属性改变时调用它。

3) UpdateSourceTrigger=PropertyChanged. If you wan't it on your datacontext you would have to explicitly set a property which would return itself and call it when the name property changes.

如下所示:

CS:

public class Entity : INotifyPropertyChanged
{
    private string _name;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            PropertyChanged(this, new PropertyChangedEventArgs("Self"));
        }
    }


    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            _firstName = value;
            PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
            PropertyChanged(this, new PropertyChangedEventArgs("Self"));
        }
    }


    public Entity Self
    {
        get { return this; }
    }


    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}

XAML:

     <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}" >
          <Setter Property="Background" Value="{Binding Self,Converter={StaticResource NameToBrushConverter}}"/>
      </Style>

但是这不会对LostFocus进行评估,但是由于它不需要它将评估第一次,然后对任何更改名称。

But this won't evaluate on LostFocus, but you wouldn't need it to any ways since it will evaluate the first time and then on any changes to name.

这篇关于WPF DataGrid编辑后更新单元格样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆