WPF Update Binding when Bound directly to DataContext w/ Converter
Asked Answered
I

3

8

Normally when you want a databound control to 'update,' you use the "PropertyChanged" event to signal to the interface that the data has changed behind the scenes.

For instance, you could have a textblock that is bound to the datacontext with a property "DisplayText"

<TextBlock Text="{Binding Path=DisplayText}"/>

From here, if the DataContext raises the PropertyChanged event with PropertyName "DisplayText," then this textblock's text should update (assuming you didn't change the Mode of the binding).

However, I have a more complicated binding that uses many properties off of the datacontext to determine the final look and feel of the control. To accomplish this, I bind directly to the datacontext and use a converter. In this case I am working with an image source.

<Image Source="{Binding Converter={StaticResource ImageConverter}}"/>

As you can see, I use a {Binding} with no path to bind directly to the datacontext, and I use an ImageConverter to select the image I'm looking for. But now I have no way (that I know of) to tell that binding to update. I tried raising the propertychanged event with "." as the propertyname, which did not work.

Is this possible? Do I have to wrap up the converting logic into a property that the binding can attach to, or is there a way to tell the binding to refresh (without explicitly refreshing the binding)?

Any help would be greatly appreciated. Thanks! -Adam

Inefficacy answered 19/4, 2010 at 20:27 Comment(0)
I
21

The workaround here was to add a property to my object (to be used as the datacontext) called "Self" , which simply returned

public Object Self { get { return this; }}

Then in the binding I used this property:

<Image Source="{Binding Path=Self, Converter={StaticResource ImageConverter}}"/>

Then when I call

PropertyChanged(this, new PropertyChangedEventArgs("Self"))

it works like a charm.

Thanks all.

Inefficacy answered 19/4, 2010 at 21:3 Comment(2)
THANK YOU! I was mad looking for this and was about to ask it when it appeared on my suggestionsFungoid
@Adam, what an awesomely elegant hack! Kudos!Shirelyshirey
P
2

I don't believe there is a way of accomplishing exactly what you need with your current converter. As you mentioned, you could do the calculation in your ViewModel, or you could change your converter into an IMulitValueConverter.

From your specific scenario (the converter tied to a ViewModel class, and a few of its properties), I would lean towards implementing the logic in the ViewModel.

Photofinishing answered 19/4, 2010 at 20:54 Comment(0)
D
0

Hmm, you don't show the full implementation. But I think it should update, if the value bound to the GUI provides the PropertyChanged-Event.

Regards

Damien answered 19/4, 2010 at 20:38 Comment(2)
The problem is that the image source is bound directly to the datacontext, not a property of the datacontext. The converter takes in the datacontext and returns an image source. To clarify, the update occurs (works) if I use a binding that has a path to a property of the datacontext, and then call PropertyChanged for that property. However, I need the object itself to determine the image (more than just a property). Thanks.Inefficacy
had a thought while typing that out. I'm going to try to put a property on my object called "Self" which just returns itself, and then call PropertyChanged("Self"). I'll give this a shot.Inefficacy

© 2022 - 2024 — McMap. All rights reserved.