In a MVVM model, my view uses a value converter. This value converter has a property X that influences the converted value. This property X might change
Whenever X changes, all values that were converted using this value converter need to be updated. Of course My ViewModel does not know that my Views use this converter, so It can't notify PropertyChanged. Besides I think it is not neat to let the ViewModel know in what format values are converted.
My value converter does not know for which values it is used. Luckily my XAML and its code behind class do.
So, my view has two converters as resources, and two text blocks that use these resources:
MyView.XAML:
<UserControl.Resources>
<convert:FormattedStringConverter x:Key="SelectedHistoryConverter" />
<vm:TimeFrameConverter x:Key="TimeFrameConverter"/>
</UserControl.Resources>
...
<TextBlock Height="20" Name="HistoryTime"
Text="{vm:CultureInfoBinding Path=SelectedHistoryTime,
Converter= {StaticResource SelectedHistoryConverter},
ConverterParameter='History Time: {0:G}'}"/>
<TextBlock Height="20" Name="Timeframe"
Text="{vm:CultureInfoBinding Path=Timeframe,
Converter= {StaticResource TimeFrameConverter},
ConverterParameter='Time Frame: [{0:G}, {1:G}]'}"/>
Event handler in MyView.XAML.cs
private void OnMyParameterChanged(object sender, EventArgs e)
{
UpdateConverterParameters(); // updates the changed property in the converters
UpdateTargets(); // forces an Update of all Targets that use these converters
}
In UpdateTargets I need to tell the two TextBlocks to update their values, which will use the changed converters.
For this I used the accepted answer in StackOverflow: How to force a WPF binding to refresh?
public void UpdateTargets()
{
BindingExpression historyTimeExpression = HistoryTime.GetBindingExpression(TextBlock.TextProperty);
historyTimeExpression.UpdateTarget();
BindingExpression timeframeExpression = Timeframe.GetBindingExpression(TextBlock.TextProperty);
timeframeExpression .UpdateTarget();
}
This works fine. However this means that whenever I add an element in XAML that uses this binding I'll have to add this element to UpdateTargets.
Is there a way for a class Derived from Binding to know which Targets are bound to it?