0

我不确定这是否可能。我已经创建了一个这样的组合框......

<ComboBox Name="testType"
          Margin="0,5,0,0"
          IsTextSearchEnabled="True"
          DisplayMemberPath="Description"
          SelectedValue="{Binding MyClass.Id, Mode=TwoWay}"
          SelectedValuePath="Id"/>

后面的代码会加载可用的选项...

        DataTable testCatList = TestTypeBase.GetAll();
        testType.ItemsSource = testCatList.DefaultView;

以便正确显示 MyClass 中的所有项目。当用户做出选择时,MyClass 中的 Id 字段会按预期进行更新。伟大的。

这是我的问题:我的 testCatList 包含每一行的 Id 和 Description,我希望这两个字段都绑定到当前的 MyClass 实例。所以这就是我尝试过的:

<ComboBox Name="testType"
          Margin="0,5,0,0"
          IsTextSearchEnabled="True"
          DisplayMemberPath="Description">
    <ComboBox.SelectedValue>
        <MultiBinding Converter="{???}">
            <Binding Path="MyClass.Id" Mode="TwoWay"/>
            <Binding Path="MyClass.Description" Mode="TwoWay"/>
        </MultiBinding>
    </ComboBox.SelectedValue>
</ComboBox>

在这里,我想将 MyClass.Id 设置为选定的 Id,并将 MyClass.Description 设置为选定的描述。如您所见,我已经删除了 SelectedValuePath,因为我不再需要 Id。但我不知道转换器使用什么(见上面的问号)。

任何想法,哦 StackOverflow 专家?谢谢。

4

1 回答 1

0

如果我理解正确,请尝试使用SelectedItemproperty 而不是SelectedValue. 这样,您将获得选定的MyClass实例,当然还有它的所有属性。

SelectedItem="{Binding CurrentSelectedMyClassItem}"
于 2012-02-08T18:31:01.773 回答