views:

42

answers:

1

I have the folowwing C# code in the .cs file and I want to move it to the XAML. the code includes property called: OperationConverte

Binding binding1 = new Binding("DisplayNames") 
    { 
       Source = OperationConverter, 
    }; 
    ComboBox4.SetBinding(ComboBox.ItemsSourceProperty, binding1); 


    Binding binding2 = new Binding("Operation") 
                      { 
                         Mode = BindingMode.TwoWay, 
                         Converter = (OperationConverter as IValueConverter) 
                      }; 
    ComboBox4.SetBinding(ComboBox.SelectedValueProperty, binding2);     

So what I've done is that:

   <ComboBox Name="ComboBox4" MinWidth="100"  ItemsSource="{Binding Path=OperationConverter.DisplayNames}" 
                               SelectedValue="{Binding Path=Operation, Mode=TwoWay, 
                               Converter={?????DONT KNOW WHAT TO DO HERE????}}" Margin="30,123,83,148" /> 

but I don't realize how to connect the OperationConverter

+2  A: 

There are multiple ways to do it but the standard practice is to declare an instance of the converter as a Resource and reference it.

<Window.Resources>
    <local:OperationConverter x:Key="MyConverter" />
</Window.Resources>

and the updated binding

SelectedValue="{Binding Path=Operation, Mode=TwoWay, Converter={StaticResource MyConverter}}"

The way you are currently exposing the converter would better be handled by not having implementing IValueConverter at all and instead exposing the converted value from the OperationConverter object and binding directly to that property.

John Bowen
Sorry But I didn't understand the :"exposing the converted value from the OperationConverter object and binding directly to that property."
Erez