简体   繁体   中英

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. 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?

Here's a different way to do it in XAML. The important part is the TextSearch.TextPath. This will search for the object with the specified name. In this case it is the string called "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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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