none
IValueConverter, pass calling object as ConverterParameter

    Question

  • Is there a way to pass the actual object that requests the conversion in IValueConterter.Convert function?

    I want to make a converter that should rely on 3 different properties of a custom usercontrol to make the appropriate conversion.
    It would be very handy for me to have the actual control in the parameter object of convert.

    Any other ideas on how to achieve that would be also welcome.

    Monday, September 15, 2008 11:34 AM

Answers

  • Hi,

    The code snippet you provided will not work because the ConverterParameter is not a dependency property so you cannot provide a value via data binding.

    You can use a multi-binding, have your value converter implement the via the IMultiValueConverter interface. The converter takes an object array, each of which can be a binding within your XAML. E.g.:

    <TextBox> 
       <TextBox.Text> 
          <MultiBinding Converter="{StaticResource MyConverter}"
              <MultiBinding.Bindings> 
                 <Binding Path="SomeProperty" /> 
                 <Binding RelativeSource="{RelativeSource Self}"/> 
              </MultiBinding.Bindings> 
           </MultiBinding> 
       </TextBox.Text> 
    </TextBox> 

    NOTE: The above was coded in notepad - so might not be teh exact syntax you are after!

    Hope that helps,
    Colin E.


    • Proposed as answer by Colin Eberhardt Monday, September 15, 2008 3:07 PM
    • Marked as answer by entrodus Tuesday, September 16, 2008 8:56 AM
    Monday, September 15, 2008 3:06 PM

All replies

  • Hi,

    Yes, this is possible, you can pass anything that you are able to bind to. In your case you can probably use a RelativeSelf binding:

    MyProperty="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource MyValueConverter}}"

    Hope this helps,
    Colin E.
    • Proposed as answer by Colin Eberhardt Monday, September 15, 2008 12:34 PM
    • Unproposed as answer by entrodus Monday, September 15, 2008 1:28 PM
    Monday, September 15, 2008 12:34 PM
  • Colin Eberhardt said:

    Hi,

    Yes, this is possible, you can pass anything that you are able to bind to. In your case you can probably use a RelativeSelf binding:

    MyProperty="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource MyValueConverter}}"


    Hope this helps,
    Colin E.



    Hi Colin.

    I think it should be more like :

    <TextBox Text="{Binding Path=SomeProperty,Converter={StaticResource MyConverter}, ConverterParameter={RelativeSource Self}}"/> 

    because i dont want to lose the actual binding. (SomeProperty is a property of my bussines object)

    Still that doesnt work... i get a parameter but it is a RelativeSource type of parameter....

    Any ideas?
    Monday, September 15, 2008 1:57 PM
  • Hi,

    The code snippet you provided will not work because the ConverterParameter is not a dependency property so you cannot provide a value via data binding.

    You can use a multi-binding, have your value converter implement the via the IMultiValueConverter interface. The converter takes an object array, each of which can be a binding within your XAML. E.g.:

    <TextBox> 
       <TextBox.Text> 
          <MultiBinding Converter="{StaticResource MyConverter}"
              <MultiBinding.Bindings> 
                 <Binding Path="SomeProperty" /> 
                 <Binding RelativeSource="{RelativeSource Self}"/> 
              </MultiBinding.Bindings> 
           </MultiBinding> 
       </TextBox.Text> 
    </TextBox> 

    NOTE: The above was coded in notepad - so might not be teh exact syntax you are after!

    Hope that helps,
    Colin E.


    • Proposed as answer by Colin Eberhardt Monday, September 15, 2008 3:07 PM
    • Marked as answer by entrodus Tuesday, September 16, 2008 8:56 AM
    Monday, September 15, 2008 3:06 PM
  • Hi Colin,

    I can see that multibinding could be the solution but i have some problems with the actual XAML code.
    I managed to make it work for 2 normal properties of my Custom bussines object but cant really find a way to reference the current object (textbox) in a binding...

    Here is what i have so far:

        <!--this works--> 
            <TextBox> 
                <TextBox.Text> 
                    <MultiBinding Converter="{StaticResource TestMultiConverter}">  
                        <Binding Path="SomeProperty"/>  
                        <Binding Path="SomeOtherProperty"/>  
                    </MultiBinding> 
                </TextBox.Text> 
            </TextBox> 
     
        <!--this does not compile--> 
            <TextBox> 
                <TextBox.Text> 
                    <MultiBinding Converter="{StaticResource TestMultiConverter}">  
                        <Binding Path="SomeProperty"/>  
                        <Binding RelativeSource="{RelativeSource Self}"/>  
                    </MultiBinding> 
                </TextBox.Text> 
            </TextBox> 
     

    Any help on how to bind Self control in the second snippet?


    Edit:

    Got it!!!!

    You dont reference the actual control, instead you can reference all the needed properties of the  control... For example:

    <Binding RelativeSource="{RelativeSource Self}" Path="CustomProperty"/>  

    Great idea Colin.
    Thank you.

    • Marked as answer by entrodus Tuesday, September 16, 2008 8:56 AM
    • Unmarked as answer by entrodus Tuesday, September 16, 2008 8:56 AM
    • Edited by entrodus Tuesday, September 16, 2008 8:59 AM
    Tuesday, September 16, 2008 8:51 AM
  • Another way to do the same, is with a binding without the path, for example

    < TextBox  Text = "{Binding Converter={StaticResource MyConverter}}" />

    In this case, the coverter, will receive the actual object that requests the conversion.

    Bye!!

    Emiliano Nieto.

    Thursday, December 16, 2010 7:04 PM