C#网格绑定不更新 [英] C# grid binding not update

查看:171
本文介绍了C#网格绑定不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到集合的网格。由于某些原因,我不知道,现在当我在网格中做一些动作时,网格不会更新。



情况:当我点击网格,它增加一个在同一行的值。当我点击时,我可以调试并看到值增量,但值不会在网格中更改。 BUT ,当我点击按钮,最小化和恢复窗口,值被更新...我需要做什么来更新像之前的价值?



更新
这不解决,但我接受了这里的最佳答案。



不是解决因为它在数据来自数据库而不是从缓存中起作用。对象被序列化,并抛出事件丢失的进程。这就是为什么我建立他们回来,它的工作原理是因为我可以与他们进行交互,但是看起来它不适用于更新网格的原因。

解决方案

为了使绑定是双向的,从控制到数据源以及从数据源到控制数据源都必须以两种可能的方式之一实现属性更改通知事件:




  • 实现 INotifyPropertyChanged 接口,并在属性更改时引发事件:

      public string Name 
    {
    get
    {
    return this._Name;
    }
    set
    {
    if(value!= this._Name)
    {
    this._Name = value;
    NotifyPropertyChanged(Name);
    }
    }
    }


  • 事件发生变化时必须通知控件的每个属性。事件名称必须以 PropertyName 的形式更改:

     公共事件EventHandler NameChanged; 

    public string Name
    {
    get
    {
    return this._Name;
    }
    set
    {
    if(value!= this._Name)
    {
    this._Name = value;
    if(NameChanged!= null)NameChanged(this,EventArgs.Empty);
    }
    }
    }

    *作为笔记您的财产值是窗口最大化后的正确值,因为控件重新读取数据源中的值。



I have a grid that is binded to a collection. For some reason that I do not know, now when I do some action in the grid, the grid doesn't update.

Situation : When I click a button in the grid, it increase a value that is in the same line. When I click, I can debug and see the value increment but the value doesn't change in the grid. BUT when I click the button, minimize and restore the windows, the value are updated... what do I have to do to have the value updated like it was before?

UPDATE This is NOT SOLVED but I accepted the best answer around here.

It's not solved because it works as usuall when the data is from the database but not from the cache. Objects are serialized and threw the process the event are lost. This is why I build them back and it works for what I know because I can interact with them BUT it seem that it doesn't work for the update of the grid for an unkown reason.

解决方案

In order for the binding to be bidirectional, from control to datasource and from datasource to control the datasource must implement property changing notification events, in one of the 2 possible ways:

  • Implement the INotifyPropertyChanged interface, and raise the event when the properties change :

    public string Name 
    {
      get
      {
        return this._Name;
      }
      set
      {
        if (value != this._Name)
        {
            this._Name= value;
            NotifyPropertyChanged("Name");
        }
      }
    }
    

  • Inplement a changed event for every property that must notify the controls when it changes. The event name must be in the form PropertyNameChanged :

    public event EventHandler NameChanged;
    
    public string Name 
    {
      get
      {
        return this._Name;
      }
      set
      {
        if (value != this._Name)
        {
            this._Name= value;
            if (NameChanged != null) NameChanged(this, EventArgs.Empty);
        }
      }
    }
    

    *as a note your property values are the correct ones after window maximize, because the control rereads the values from the datasource.

这篇关于C#网格绑定不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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