4
votes

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 changes (INotifyPropertyChanged is implemented and OnPropertyChanged raised), this binding without path does not get updated. I guess because it does not bind to a specific property.

How to tell that it has to update?


I try to be more specific:

The bar object has a 'Start' property. When I change this the bar moves in time because the binding gets directly to the Start property. So the notification works for single properties. But the tooltip binding is {Binding Converter={StaticResource TooltipConverter}} and does not bind to a specific property. When 'Start' changes, the bar is moved but the tooltip does not update because the tooltipconverter is not called again.

The bar is one object in an ObservableCollection<Bar>. Should the bar tell the collection or the view model? Normally is would not have any relationship to it.

3
raise the PropertyChangedEvent with string.emptyJehof
@Jehof That does not work.ZoolWay

3 Answers

6
votes

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
votes

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
votes

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");
}