Hi. i'm new to wpf and xaml. i find solutions of most of the problems (in other languages) by searching here n there or googling around but this one made me crazy. i searched a lot on google and browsed many forums, but it seems that this time i met with a real challenge!
I have following code in Window.Resources:
<my:NameConverter x:Key="NameConverter"/>
<MultiBinding x:Key="CustomerFullNameBinding"
Converter="{StaticResource NameConverter}"
ConverterParameter="LastNameFirst">
<Binding Path="FirstName" />
<Binding Path="MiddleName" />
<Binding Path="LastName" />
</MultiBinding>
The NameConverter class returns full name by combining individual parts of the name. The return value is based on value of ConverterParameter which can be one of the following: FirstNameFirst, LastNameFirst, OnlyFirstName, OnlyLastName, Initials (there are a few more; but for simplicity, let's not consider them)
This code works fine. I use following to get the result:
<TextBlock Text="{DynamicResource CustomerFullNameBinding}"/>
However, the problem is that if i use the resource CustomerFullNameBinding 20 times in my code (like shown abovw) all of them would show the name in ONE particular format. (i.e. Here in the declaration of CustomerFullNameBinding i have set ConverterParameter="LastNameFirst" so all 20 references will show name in that way only.)
Now my question is can i make it more "generalized" so that the i can set the value of ConverterParameter dynamically? i.e. i can have multiple textblocks that can display full name in different formats. I know it is possible if we create multiple resources like follows:
<MultiBinding x:Key="CustomerFullNameBinding_FirstNameFirst"
Converter="..." ConverterParameter="FirstNameFirst">
......
</MultiBinding>
<MultiBinding x:Key="CustomerFullNameBinding_LastNameFirst"
Converter="..." ConverterParameter="LastNameFirst">
......
</MultiBinding>
<MultiBinding x:Key="CustomerFullNameBinding_OnlyFirstName"
Converter="..." ConverterParameter="OnlyFirstName">
......
</MultiBinding>
... and so on ...
But i dont want that, 'coz it'll really make my code too bulky, and most importantly it'll void concept of reusability of resources!
Please suggest me any possible solution.
Thanks in advance.
Note: I got the original idea from here. The similar working example can be found msdn.microsoft.com/en-us/library/ms771336.aspx.
Tags: wpf xaml staticresource dynamicresource binding