繁体   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