Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use converters without binding paths?

I have a Combobox whose ItemsSource is an ObservableCollection of int values. My combobox itemtemplate consists on an image and a textblock which content is given by 2 converters.

How can I set this 2 bindings? The following code does not compile:

<ComboBox.ItemTemplate>
     <DataTemplate>
          <StackPanel Orientation="Horizontal">
               <Image Source="{Binding, Converter={StaticResource IntToImageConverter}}" Stretch="None" />
               <TextBlock Text="{Binding, Converter={StaticResource IntToStringConverter}}" />
          </StackPanel>
     </DataTemplate>
</ComboBox.ItemTemplate>
like image 954
Eduardo Brites Avatar asked Jan 25 '13 14:01

Eduardo Brites


People also ask

What is IValueConverter?

The IValueConverter interface consists of two methods, Convert() and ConvertBack() . Convert method gets called when source updates target object. ConvertBack method gets called when target updates source object.

What is IValue Converter in WPF?

A Value Converter functions as a bridge between a target and a source and it is necessary when a target is bound with one source, for instance you have a text box and a button control. You want to enable or disable the button control when the text of the text box is filled or null.

What is binding path in WPF?

Binding path syntax. Use the Path property to specify the source value you want to bind to: In the simplest case, the Path property value is the name of the property of the source object to use for the binding, such as Path=PropertyName . Subproperties of a property can be specified by a similar syntax as in C#.


1 Answers

You need to remove the , so:

<ComboBox.ItemTemplate>
     <DataTemplate>
          <StackPanel Orientation="Horizontal">
               <Image Source="{Binding Converter={StaticResource IntToImageConverter}}" Stretch="None" />
               <TextBlock Text="{Binding Converter={StaticResource IntToStringConverter}}" />
          </StackPanel>
     </DataTemplate>
</ComboBox.ItemTemplate>
like image 193
CodeNaked Avatar answered Oct 04 '22 00:10

CodeNaked