23

I am trying to bind two values to a ComboBox display value, but I do not know how to do.

This way does not work:

cboRegion.DisplayMemberPath = "idregion" + "description";

Does anyone know how to do that in C#?

unairoldan
  • 2,775
  • 4
  • 28
  • 49

3 Answers3

78

Unfortunately, this is not possible with DisplayMemberPath. You have the following alternatives:

  • Specify a DataTemplate

    <ComboBox>
      <ComboBox.ItemTemplate>
        <DataTemplate>
          <TextBlock>
            <TextBlock.Text>
              <MultiBinding StringFormat="{}{0}: {1}">
                <Binding Path="idregion"/>
                <Binding Path="description"/>
              </MultiBinding>
            </TextBlock.Text>
          </TextBlock>
        </DataTemplate>
      </ComboBox.ItemTemplate>
    </ComboBox>
    

    (If you are wondering about the empty braces in the StringFormat attribute, see: What do the {} brackets mean in the StringFormat section of a Binding syntax?)

  • Add a property or field to your data source. How to do that depends on your data source:

    If your combo box is bound to a DataTable, add a DataColumn and fill its values in a loop. Alternatively, change your SQL and add the concatenated value to your SELECT clause.

    If your combo box is bound to a POCO or entity framework object, add a property that returns the concatenation.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Adding a property to your object and setting the concatenated values there is a super easy way to go... – nikotromus Feb 28 '18 at 17:55
  • 1
    With IsEditable=true ItemTemplate will only work for dropdown, not for the selected value. Combobox will still call `SelectedValue.ToString()` and display type name if you didn't override it. – Konrad Jun 19 '19 at 09:55
23

You need to use a DataTemplate:

<ComboBox Name="cboRegion">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Run Text="{Binding idregion}" />
                <Run Text="{Binding description}" />
            </TextBlock>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Is there no way to do it in C#, not in XAML? – unairoldan Apr 17 '12 at 08:13
  • 1
    @Ztere0: You could create the DataTemplate in C# too, see this question: http://stackoverflow.com/questions/248362/how-do-i-build-a-datatemplate-in-c-sharp-code. Note, however, that the XAML code is easier and more readable in this case. – Heinzi Apr 17 '12 at 08:16
  • @Ztere0, yes, but it's not convenient at all... why would you want to do it in C#? – Thomas Levesque Apr 17 '12 at 08:49
  • 1
    Thanks for that. WPF positively surprises me on a daily basis :-) – Endrju May 27 '14 at 19:20
  • With IsEditable=true ItemTemplate will only work for dropdown, not for the selected value. Combobox will still call `SelectedValue.ToString()` and display type name if you didn't override it. – Konrad Jun 19 '19 at 09:55
1

You could create a view, concatenate the two fields, and then refer to the concatenated field name in your DisplayMemberPath property in c#, after referring to the new view in your itemssource property (and after updating your entity framework model)