简体   繁体   English

如何有一个下拉列表显示两列但选择项仅显示一列的组合框?

[英]How to have a combobox where the drop down shows two columns but the selection shows only one?

I would google this, but I have no idea how to word it to do a search. 我会用谷歌搜索,但是我不知道如何用词表达来进行搜索。 My problem is pretty simple: I'm porting an application written in Access, and on one of the forms is a combobox. 我的问题很简单:我正在移植用Access编写的应用程序,并且在其中一种表单上是一个组合框。 When you open the dropdown, it displays two columns of info: abbreviations on the left and full names on the right. 当您打开下拉列表时,它将显示两列信息:左侧为缩写词,右侧为全名。 When you select one though, the selected option in the combobox (the dropdown has closed) shows only the abbreviation. 但是,当您选择一个时,组合框中的选定选项(下拉菜单已关闭)仅显示缩写。 Any idea how to achieve this in WPF? 任何想法如何在WPF中实现这一目标?

Here's a different way to do it in XAML. 这是在XAML中执行此操作的另一种方法。 The important part is the TextSearch.TextPath. 重要的部分是TextSearch.TextPath。 This will search for the object with the specified name. 这将搜索具有指定名称的对象。 In this case it is the string called "Foo". 在这种情况下,它是称为“ Foo”的字符串。

    <ComboBox Name="cmbBox" ItemsSource="{Binding Test}" Height="25" IsEditable="True" IsReadOnly="True" TextSearch.TextPath="Foo">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" TextSearch.Text="{Binding Path=Bar}">
                    <TextBlock Text="{Binding Path=Foo}"/> 
                    <TextBlock Text="{Binding Path=Bar}" Margin="10 0"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

To set the TextSearch Programmatically all you have to do is: 要以编程方式设置TextSearch,您需要做的是:

    cmbBox.SetValue(TextSearch.TextPathProperty, "Foo");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM