Tutorial :wpf shared ResourceDictionary file parameters?



Question:

if all Styles and Converters are stored in shared resource dictionary file (styles.xaml), and this file is used from various windows.

Is it possible, to pass a parameter to that file, and propagate that parameter to the converters?

I am looking for a way to pass a "origin" type parameter, so that the converters could be aware which place they are being used from? Just a hint of which window/grid is using the converter at the moment..


Solution:1

See this article for Converters with parameters. I assume you define the Converter resource in Resource dictionary.

http://www.switchonthecode.com/tutorials/wpf-tutorial-binding-converters


Solution:2

I wonder if something like this will work:

Add a reference to the System namespace in the declaration of each Window or UserControl where you want this.

     xmlns:sys="clr-namespace:System;assembly=mscorlib"  

Then in your resources section set things up like this:

<Window.Resources>      <ResourceDictionary>          <ResourceDictionary.MergedDictionaries>              <ResourceDictionary Source="styles.xaml"/>          </ResourceDictionary.MergedDictionaries>              <sys:String x:Key="WinConvertParam">IDTextForThisWindow</sys:String>      </ResourceDictionary>  </Window.Resources>  

Your binding syntax could then look something like this:

{Binding SomeProperty,            Converter={StaticResource thatConverterIWrote},            ConverterParameter={StaticResource WinConvertParam}}  

...and your Convert or ConvertBack methods in your conversion classes then become aware of the Window that's using them, provided you vary the value of that <sys:String/> from file to file.

What do you think?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »