簡體   English   中英

如何有一個下拉列表顯示兩列但選擇項僅顯示一列的組合框?

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

我會用谷歌搜索,但是我不知道如何用詞表達來進行搜索。 我的問題很簡單:我正在移植用Access編寫的應用程序,並且在其中一種表單上是一個組合框。 當您打開下拉列表時,它將顯示兩列信息:左側為縮寫詞,右側為全名。 但是,當您選擇一個時,組合框中的選定選項(下拉菜單已關閉)僅顯示縮寫。 任何想法如何在WPF中實現這一目標?

這是在XAML中執行此操作的另一種方法。 重要的部分是TextSearch.TextPath。 這將搜索具有指定名稱的對象。 在這種情況下,它是稱為“ 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>

要以編程方式設置TextSearch,您需要做的是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM