Multiple ComboBox.DisplayMemberPath parameters?

I am binding a collection of objects to ComboBox

. What I want to achieve is a combobox that displays two properties of my object. However, I have not figured out how to make the display of the combobox somewhat DisplayMemberPaths

. Is it possible?

This is how I currently configure the binding and set DisplayMemberPath

:

Binding comboBinding = new Binding();
comboBinding.Source = squad_members; //squad_members is the object collection

BindingOperations.SetBinding(Character_ComboBox, ComboBox.ItemsSourceProperty, comboBinding);
Character_ComboBox.DisplayMemberPath = "Name"; //
//Character_ComboBox.DisplayMemberPath = "Name" + "Age"; //failed attempt
Character_ComboBox.SelectedValuePath = "Name";

      

+3


source to share


4 answers


First of all, you must do the binding in XAML instead of code.

You can provide and use in the following way: ItemTemplate

StringFormat

MultiBinding

<ComboBox ItemsSource="{Binding YourCollection}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0} - {1}">
                        <Binding Path="Name"/>
                        <Binding Path="Age"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

      


If you want to make the binding still in code.



You can opt out of setting DisplayMemberPath entirely by overriding the ToString () method in the base original class. As internally ToString () is called if DisplayMemberPath is not provided.

Let's assume the collection is of type List<Person>

, so you can override ToString () in the Person class:

public override string ToString()
{
    return Name + Age;
}

      

With this in place, the binding would look like this (DisplayMemberPath is not required)

Binding comboBinding = new Binding();
comboBinding.Source = squad_members; //squad_members is the object collection

BindingOperations.SetBinding(Character_ComboBox,
                   ComboBox.ItemsSourceProperty, comboBinding);

      

+9


source


Use the ItemTemplate of your ComboBox, use a lot of TextBlock, as many columns as you need. Something like that,



<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Width="50" Text="{Binding Path=Name}" />
<TextBlock Width="50" Text="{Binding Path=Age}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>

      

+3


source


In addition to the other answer, you can either get a property that returns what you want, or better, a converter that will do the job for you.

Prop:

public int First {get; set;}
public int Second {get; set;}
public int BindToThis {get{return First+Second;}}

      

Converter: See example on this MSDN page .

Mostly along the lines:

<TextBlock Name="textBox2" DataContext="{StaticResource NameListData}">
  <TextBlock.Text>
    <MultiBinding Converter="{StaticResource myNameConverter}"
                  ConverterParameter="FormatLastFirst">
      <Binding Path="FirstName"/>
      <Binding Path="LastName"/>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

      

and

public class NameConverter : IMultiValueConverter
{
    public object Convert( object[] values
                         , Type targetType
                         , object parameter
                         , CultureInfo culture )
    {
        string name;

        switch ((string)parameter)
        {
            case "FormatLastFirst":
                name = values[1] + ", " + values[0];
                break;
            case "FormatNormal":
            default:
                name = values[0] + " " + values[1];
                break;
        }

        return name;
    }

    //  public object[] ConvertBack...
}

      

+1


source


Or, you just override the ToString method in your class for example.

    public override string ToString()
    {
        return Name + " " + Age;
    }

      

0


source







All Articles