Converter limitations

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

 




Answer this question

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 &amp;#40;TR-NL&amp;#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

    This might be overkill, but you could make a class which exposes an attached property. On each TextBox in the XAML you set the attached property. The property is set to something like this MyAttachedProperty="Attribute1=OptionA, Attribute2=OptionB, Attribute3=OptionC". In the class which exposes the attached property, you have logic which parses that input value and configures a ValueConverter to use the specified options/settings. It then would retrieve a reference to the TextBox's Text property Binding and set its Converter property to that configured ValueConverter.

  • 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

    Am I limited to one attached property

  • Mazmo

    No. If you have a definite number of properties on the Converter and don't intend on reusing the converter factory, then having separate attached properties for each Converter property would be the way to go. I guess I always think in terms of generalized solutions! :)

  • 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

    e. MyNameSpace:MyConverter.Property

  • 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



  • Converter limitations