开发者

WPF datagrid refreshes on it's own when it's not supposed to

开发者 https://www.devze.com 2023-03-07 06:58 出处:网络
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 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.UpdateSourceTr开发者_StackOverflowigger = 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号