cost
cost

Reputation: 4490

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?

Upvotes: 1

Views: 1108

Answers (1)

Bambu
Bambu

Reputation: 548

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");

Upvotes: 4

Related Questions