Is there any way I can set properties on a Converter.
For example: We have a lot TextBox controls that deal with floating point values. I would like my UI guy to be able to designate the number of digits of precision should be displayed in a given control instance because he is the one that makes that decision.
As it is, it looks like I would have to provide a different converter for each digit depth.
OneDigitPrecisionConverter
TwoDigitPrecisionConverter
ThreeDigitPrecisionConverter
That isn't too bad, but as I add additional attributes, the quantity of Converter objects spins out of control quickly (eg. 27 permutations for 3 attributes with 3 options each).
Any suggestions on how best to deal with this
NIK

Converter limitations
blah569
Doug,
In his original post I believe he mentioned that he didn't want to create an instance for each permutation of property settings. My solution would allow him to avoid creating any instance in XAML. I like your idea of using the ConverterParameter. Perhaps one could use the technique I presented, but pass the converter config values in the ConverterParameter instead of making a separate class with an attached property, etc. The converter could configure its properties when it's used based the values in the ConverterParameter value. Very interesting...
KDX
Nick,
Setting an attribute in XAML does not create the property in the class (i.e. the C# code). You mentioned in your first post on this thread that the converter class has multiple properties which can be configured. Those are the properties I was suggesting you set. If you set a property in XAML, that property must exist in the real class first.
fabianus
1. Doh! - I knew that, sorry
2. Still a little fuzzy on this part.
a) Is this declaring the existence of the property to the XAML compiler
b) Do I have to implement the property on the converter object to back it up
c) Why the x:key name
d) Does this affect all instances of the converter object
e) Compile error with the syntax given
<
MyNameSpace:MyConverter x:Key="conv1" Property="Something" />Error 5 The property 'Property' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
3. Ok
4.
Ayhan Yerli &#40;TR-NL&#41;
Understood, it just wasn't clear at first glance what was assumed to exist (or to be understood by me).
Ok, so everyone gets a helpful post mark.
Thanks again.
Dinesh Patel
su45937
Thanks for the response folks:
I don't understand the XMAL here.
1) MyConverter is not a valid tag, so it won't compile like this.
2) Does Property="Something" create a property on the converter
3) if 2, how do I set the property in XAML
4) How do I designate data type for the property
toben888
Mazmo
smileyke
You can set any properties you want on the converter when you instantiate it:
eg.
<MyConverter x:Key="conv1" Property="Something" />
<MyConverter x:Key="conv2" Property="Something Else" />
Or you can use the ConverterParameter in the binding - but this is restricted to one value.
{Binding Converter={StaticResource ...}, ConverterParameter=Something}
- Doug
Ian_E
sunny123
1. you should include namsspace <local:MyConverter.../>
2. yes
3. you can set it like you set any property <local:MyConverter myproperty=""/>
Girishbs
Ok, I think my brain just finally grocked all this, the partial XAML threw me off a bit.
I implemented a property (DigitsOfPrecision) on my converter class (MyDoubleConverter).
The following declaration goes in the Windows.Resources section.
<MyNamespace:MyDoubleConverter
x:Key="DoubleConverter" MyNamespace:MyDoubleConverter.DigitsOfPrecision="2"/>Then, my binding just references the object "DoubleConverter" as it did before.
<Binding Path="Value" Source="{StaticResource ExposureTime}" Converter="{StaticResource DoubleConverter}" UpdateSourceTrigger="PropertyChanged"/>
This works.
Thanks for the communal effort