的DisplayMemberPath级联级联、DisplayMemberPath

2023-09-03 06:06:46 作者:孤独噬心

我想两个值绑定到一个组合框显示值,但我不知道该怎么办。

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

这个方法不起作用:

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

有谁知道该怎么做,在C#?

Does anyone know how to do that in C#?

推荐答案

不幸的是,这是不可能的的DisplayMemberPath 。您有以下选择:

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

指定一个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>

添加属性或字段到数据​​源。如何做到这一点取决于你的数据源:

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

如果您的组合框绑定到一个DataTable,在一个循环中添加的DataColumn,并填写自己的价值观。另外,改变你的SQL和连接的值添加到您的 SELECT 条款。

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.

如果您的组合框绑定到POCO或实体框架对象,添加返回串联的属性。

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