WPF 数据网格在不应该刷新时自行刷新

发布于 2024-11-09 06:40:43 字数 1657 浏览 2 评论 0原文

我觉得我在这里遗漏了一些东西,但是我有这个数据网格,当数据源更改时,它会自动重新绘制它,而没有任何逻辑原因这样做。

我将数据网格绑定到实现 INotifyPropertyChanged 的​​ DataView 属性,并且我想在调用 Refresh() 之前触发该事件时执行一些其他操作。

所以这是数据源。

public class MainScreenDataView : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    DataView _dataview;

    public DataView GetDataView
    {
        get { return _dataview; }
        set
        {
            _dataview = value;
            OnPropertyChanged("GetDataView");
        }
    }
    public MainScreenDataView()
    {
    }
}

并且绑定(我在窗口的构造函数中称之为)

    public void MakeData()
    {
        MiddleMan midman = MiddleMan.Instance; 
        midman.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(midman_PropertyChanged); //unrelated event for error messages
        midman.InstantiateAll();


        Binding bind = new Binding();
        bind.Source = midman.GetDict["contact"].GetDataView; //GetDict is a dictionary that holds instances of MainScreenDataView
        bind.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
        DG_Contacts.SetBinding(BetterDataGrid.ItemsSourceProperty, bind);
    }

使用数据库中的数据更新 DataView 的类可以访问与窗口相同的 MainScreenDataView 实例。该实例以单例形式保存在字典中。

现在我不明白为什么数据网格会自行刷新,我什至尝试从 MainScreenDataview 中删除 INotifyPropertyChanged 内容,但它保持相同的行为。

猜猜我在这里缺少一些东西。某个地方的默认行为需要被覆盖还是什么?

I feel like I'm missing something here, but I have this datagrid which when the datasource changes, automatically redraws it self with no logical reason for it doing so.

I have the datagrid bound to a DataView property which implements INotifyPropertyChanged and I want to do some other stuff when that event is fired before calling Refresh().

So here is the datasource.

public class MainScreenDataView : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    DataView _dataview;

    public DataView GetDataView
    {
        get { return _dataview; }
        set
        {
            _dataview = value;
            OnPropertyChanged("GetDataView");
        }
    }
    public MainScreenDataView()
    {
    }
}

And the binding (I call this in the constructor of the window)

    public void MakeData()
    {
        MiddleMan midman = MiddleMan.Instance; 
        midman.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(midman_PropertyChanged); //unrelated event for error messages
        midman.InstantiateAll();


        Binding bind = new Binding();
        bind.Source = midman.GetDict["contact"].GetDataView; //GetDict is a dictionary that holds instances of MainScreenDataView
        bind.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
        DG_Contacts.SetBinding(BetterDataGrid.ItemsSourceProperty, bind);
    }

The class that updates the DataView with data from a database has access to that same instance of MainScreenDataView as the window. The instance is kept in a dictionary in a singleton.

Now I see no reason why the datagrid would refresh it self, I even tried removing the INotifyPropertyChanged stuff from MainScreenDataview and yet it keeps the same behavior.

Guess there's something I'm missing here. Default behavior somewhere that needs to be overridden or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2024-11-16 06:40:43

您已经交换了目标和源。我自己做的。 UpdateSourceTrigger.Explicit 设置影响绑定更新的方式,源是 MainScreenDataView.GetDataView 属性,而不是 DataGrid.ItemSource. DataGrid.ItemSource目标

MainScreenDataView 中删除 INotifyPropertyChanged 不会对单例产生任何影响,因为实例不会改变,只会改变实例内的值。换句话说,GetDataView 是一个“设置后就忘记它”的属性。

只要绑定有效,就无法阻止绑定系统传播对集合所做的更改,除非您禁止触发或阻止 DataView.CollectionChanged 事件,以便绑定子系统根本不运行。

如果您确实想要这样做,您可以断开绑定并在准备好时再次设置它,或者创建一个全新的 DataView 并在准备好时覆盖绑定。

You've got target and source swapped. Done it myself. The UpdateSourceTrigger.Explicit setting affects how the binding updates the source which is the MainScreenDataView.GetDataView property not the DataGrid.ItemSource. The DataGrid.ItemSource is the target.

Removing INotifyPropertyChanged from MainScreenDataView will have no effect on a singleton because the instance doesn't change, only the values inside the instance. In other words, GetDataView is a "set it and forget it" property.

As long as the binding is in effect, there is no way to prevent changes made to the collection from being propagated by the binding system unless you suppress DataView.CollectionChanged events from firing or block so that the binding subsystem simply doesn't run.

If you really want this you can disconnect the binding and set it again when you are ready or create an entirely new DataView and overwrite the binding when you are ready.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文