none
Binding Values Y, N or Yes, No or 1, 0 to a WPF checkbox

    Pregunta

  • Hi ,

    I have a scenario where my customized checkbox should be able to bind values (IsChecked) anything ex: Y, N or 1, 0 as Booleans. Currently I have implemented the IValueConverter interface and overrided the Convert and ConvertBack method adding my logic to convert the different values into bool appropriately. This works fine. 

    But now what I need to do is instead of putting this logic into the class file of customized checkbox, I would like to have all this code in my xaml file itself where I can using Templates configure different values as Booleans. Can you please guide me in achieving the same.

    Thanks in Advance.

    sábado, 11 de octubre de 2008 7:45

Respuestas

  • Hi,

    I am still a little unsure about what you want. You do not wish to use ConverterParameter? In that case, you can make teh parameter a property of the converter and create an instance within your resources. E.g....

    <Window.Resources>
      <l:BooleanConverter x:Key="YesNoConverter" TrueValue="Y" FalseValue="N"/>
      <l:BooleanConverter x:Key="OneZeroConverter" TrueValue="1" FalseValue="0"/>
    </Window.Resources>

    These can then be referenced within your bindings.

    Colin E.

    Regards, Colin E. -- Please mark any posts that answer your questions as an 'answer'
    • Marcado como respuesta Toreador viernes, 17 de octubre de 2008 7:15
    jueves, 16 de octubre de 2008 7:25

Todas las respuestas

  • Hi,

    There are a few options available to you. You could pass the values that represent Y/N as a converter parameter:

    <CheckBox IsChecked="{Binding Path=StringProperty, Converter={StaticResource BooleanConverter},
      ConverterParameter='Y;N'}" />
    <CheckBox IsChecked="{Binding Path=IntProperty, Converter={StaticResource BooleanConverter},
      ConverterParameter='0;1'}" />

    For example, the above value converter could parse the ConverterParameter,splitting at the semicolon, to determine the values that represent true / false. Type conversion should not be a problem, within your ValueConverter implementation simply compare teh ToString() value of the value being converted.

    Alternatively, you could define your ValueConverter as a Resource, this will allow you to write a ValueConverter with dependency properties to represent the true / false values. See the followgin example:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7298ceb5-bf56-47aa-a161-5dd99189408b/

    Personally I think teh first option is the best. Let me know how it goes.
    Colin E.
    • Propuesto como respuesta Colin Eberhardt sábado, 11 de octubre de 2008 10:36
    sábado, 11 de octubre de 2008 10:36
  • Hi Colin,

    Thanks for your reply.

    The thing is i can't use the ValueConverters. In my case the Convertparameter can be anything those are configurable. And for each case i need to modify or add logic to ValueConverter implementation which i need to avoid. I was thinking of a way in which we can specify those values in xaml itself. Alternatively can we make use of DataTriggers ?

    Thanks in advance.

     

    domingo, 12 de octubre de 2008 7:47
  • Hi Colin,

    Can we bind the Converter to an ObjectDataProvider which may be written in a seperate class file?

    Thanks
    miércoles, 15 de octubre de 2008 7:22
  • Hi,

    I am still a little unsure about what you want. You do not wish to use ConverterParameter? In that case, you can make teh parameter a property of the converter and create an instance within your resources. E.g....

    <Window.Resources>
      <l:BooleanConverter x:Key="YesNoConverter" TrueValue="Y" FalseValue="N"/>
      <l:BooleanConverter x:Key="OneZeroConverter" TrueValue="1" FalseValue="0"/>
    </Window.Resources>

    These can then be referenced within your bindings.

    Colin E.

    Regards, Colin E. -- Please mark any posts that answer your questions as an 'answer'
    • Marcado como respuesta Toreador viernes, 17 de octubre de 2008 7:15
    jueves, 16 de octubre de 2008 7:25
  • Thanks Colin this was what i was trying to achieve. Thanks once again.
    viernes, 17 de octubre de 2008 7:16