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

标签 c# wpf data-binding datagrid dataview

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

我将数据网格绑定(bind)到实现 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()
    {
    }
}

和绑定(bind)(我在窗口的构造函数中调用它)

    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 内容,但它保持相同的行为。

猜猜我在这里遗漏了什么。某处的默认行为需要被覆盖还是什么?

最佳答案

你已经交换了目标和源。自己做的。 UpdateSourceTrigger.Explicit 设置会影响绑定(bind)如何更新 source,即 MainScreenDataView.GetDataView 属性而不是 DataGrid.ItemSourceDataGrid.ItemSource目标

MainScreenDataView 中删除 INotifyPropertyChanged 对单例没有影响,因为实例不会改变,只会改变实例中的值。换句话说,GetDataView 是一个“设置后不用管”的属性。

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

如果你真的想要这个,你可以断开绑定(bind)并在准备好时重新设置它,或者创建一个全新的 DataView 并在准备好时覆盖绑定(bind)。

关于c# - WPF 数据网格在不应该刷新时自行刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6084290/

相关文章:

c# - 如何在 WP7 应用程序中循环显示多个图像?

c# - 子网格更新时刷新父 Kendo MVC 网格

.net - 从 WPF Windows 应用程序使用具有集成身份验证的 WebService

c# - 如何在 c# wpf 中使用本地数据库 .sdf?

WPF 绑定(bind)到一个不变的属性

c# - WPF 设计器在启动时不运行窗口静态构造函数

c# - 如何编码 Time > 8 :15

c# - WPF 复制将正多边形混合到应用程序中

android - 表达式中 ObservableFields 的 get()

javascript - 使用 Knockout.js foreach 创建 Bootstrap 选项卡