WPF Binding without path with converter, update

后端 未结 3 2072

I have a binding without path using a converter. This is because the converter will use many properties of the object to build the text for the tooltip. But when one property ch

相关标签:
3条回答
  • 2021-01-21 00:23

    If you implement the INotifyPropertyChange interface on a data type class, that takes care of notifying for changes to the class properties. If you implement the INotifyPropertyChange interface on a view model class that has a property of the type of your class, then that takes care of notifying for changes to that property, which in this case is the whole object.


    UPDATE >>>

    Right... I believe that you have a property of type ObservableCollection<Bar>. If you add a PropertyChanged handler to each of the items...:

    foreach (Bar item in YourCollectionProperty) item.PropertyChanged += 
    Item_PropertyChanged;
    

    ... then you can do this:

    private void Item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        NotifyPropertyChanged("YourCollectionProperty");
    }
    
    0 讨论(0)
  • 2021-01-21 00:39

    Just to make it clear: You have some DataTemplate for item in a collection. Type of item is Bar. ItemsSource for parent list is a collection of Bar objects. You want to change tooltip if any property in particular Bar object changes.

    Problem is that when you are not specifying specific path binding will subscribe to change notification of DataContext which in your case is Bar. So it does not care about changes or notifications within your object.
    You can implement bubbling so changed event will be routed to parent object, but I would prefer defining separate Changed property for such kind of things.

    public bool StateChanged
    {
       get { return true; }
    }
    

    And subscribe to PropertyChanged event within your class:

    this.PropertyChanged += PropertyChangedHandler;
    

    In handler notify that your object state changed:

    private void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
    {
        if  (!e.PropertyName.Equals("StateChanged"))
        {
            this.OnPropertyChanged("StateChanged");
        }
    }
    
    0 讨论(0)
  • 2021-01-21 00:46

    One possible workaround is:

    Give your object a ItSelf property (or other name) like:

    public Object ItSelf
    {
        get { return this; }
    }
    

    Instead of binding

    {Binding Converter={StaticResource TooltipConverter}}
    

    use

    {Binding ItSelf, Converter={StaticResource TooltipConverter}}
    

    Then you raise OnPropertyChanged for ''ItSelf'' for every property. So you can signal an update for the whole object whenever it was used in a binding.

    public DateTime Start
    {
        get { return this.start; }
        set { this.start = value; OnPropertyChanged("Start"); OnPropertyChanged("ItSelf");
    }
    

    I got this to work a bit faster but would like to test out AttachedBehavior for this like stated by @AnatoliiG so I will accept an answer later.

    0 讨论(0)
提交回复
热议问题