Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fallback value for DynamicResource

My application loads all strings from resource dictionaries defined in separate XAML files. It is possible that the DynamicResource key will not exist during run-time.

<CheckBox Content="{DynamicResource myKeyThatIsMissing}"/>

In a normal binding there is a Fallback value that can be used like this:

Text="{Binding StringToShow, FallbackValue=DefaultValue}

Is it possible to have a default Fallback value for a property set with a DynamicResource that is used if the resource key does not exist?

like image 883
burnttoast11 Avatar asked Nov 13 '22 21:11

burnttoast11


1 Answers

The class System.Windows.DynamicResourceExtension is unsealed so perhaps you can extend it and add the logic you desire by overriding the ProvideValue method.

Unfortunately the default implementation returns an instance of the internal ResourceReferenceExpression class, so you may have to do some reflection or other magic to make this work.

like image 125
Drew Noakes Avatar answered Nov 15 '22 09:11

Drew Noakes