Search code examples
c#wpflistviewtreeviewuielement

WPF gets "old" value when data in ListView change


I have the TreeView with objects. When I select item of these tree, than other control - ListView displays as its items properties of selected object. I want to save values of properties when in TreeView selection is change to other object. So, is there a good way in WPF to gets values of "just before changing" items in ListView control? My idea for now is to override the PreviewMouseDown to check if user click tree node. By god way I mean better than mine. Maybe something in ListView template? Indication that there is no need to change my idea with the PreviewMouseDown will be also good answer.


Solution

  • Could you please provide the relevant code snippets? I try to answer your question, but I'm not sure I understood it correctly. If you bind the SelectedItem of you TreeView to a property (a.e. using MVVM pattern), you can save the values before actually setting the item.

    Doing so in the setter is not so good though, because it becomes quite large then. I would have a setter like this:

    private Foo bar;
    public Foo Bar
    {
        get { return bar; }
        set
        {
            OnPropertyChanging("Bar");
            bar=value;
            OnPropertyChanged("Bar");
        }
    }
    

    Then you can listen to your own PropertyChanging events and do your stuff there:

    private void this_PropertyChanging(object param, PropertyChangingEventArgs e)
    {
        switch(e.PropertyName)
        { 
            case "Bar":
                //Do you stuff
             break,
        }
    }