簡體   English   中英

WPF ComboBox 與 object 列表綁定,並且在所選項目中顯示的值比在項目列表中的不同

[英]WPF ComboBox bound with object list and display less different value in selected item than in the item list

我有這個 ComboBox:

<ComboBox
    ItemsSource="{Binding imageFormats}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <TextBlock DockPanel.Dock="Left" Text="{Binding Extension}" />
                <TextBlock DockPanel.Dock="Left" Text=" - " />
                <TextBlock DockPanel.Dock="Right" Text="{Binding Description}" />
            </DockPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

綁定到這個列表: private List<ImageFormatModel> imageFormats = new List<ImageFormatModel>();

public MainWindow()
{
    ComboBoxImages.ItemsSource = imageFormats;
}

Object ImageFormatModel由兩個字符串組成:

public class ImageFormatModel
{
    public string Extension { get; set; }
    public string Description { get; set; }
}

所選項目是否可能僅顯示擴展名但在下拉菜單中都顯示?

這兩個值都應顯示在此菜單中:

下拉式菜單

但是如果我 select 之一,應該只有擴展名是可見的。 不像這樣: 在此處輸入圖像描述

您可以將帶有DataTriggerStyle應用於要隱藏的TextBlock元素:

<DataTemplate>
    <DockPanel>
        <DockPanel.Resources>
            <Style x:Key="tbStyle" TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="{x:Null}">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DockPanel.Resources>
        <TextBlock DockPanel.Dock="Left" Text="{Binding Extension}" />
        <TextBlock DockPanel.Dock="Left" Text=" - " Style="{StaticResource tbStyle}" />
        <TextBlock DockPanel.Dock="Right" Text="{Binding Description}" Style="{StaticResource tbStyle}" />
    </DockPanel>
</DataTemplate>

暫無
暫無

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

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