简体   繁体   English

WPF ComboBox 与 object 列表绑定,并且在所选项目中显示的值比在项目列表中的不同

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

I have this ComboBox:我有这个 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>

Which is bound to this list: private List<ImageFormatModel> imageFormats = new List<ImageFormatModel>();绑定到这个列表: private List<ImageFormatModel> imageFormats = new List<ImageFormatModel>();

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

The Object ImageFormatModel consists of two strings: Object ImageFormatModel由两个字符串组成:

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

Is possible that the selected item shows only the extension but in the dropdown menu both are shown?所选项目是否可能仅显示扩展名但在下拉菜单中都显示?

Both values should be shown in this menu:这两个值都应显示在此菜单中:

下拉式菜单

But if I select one, only the extension should be visible.但是如果我 select 之一,应该只有扩展名是可见的。 Not like this:不像这样: 在此处输入图像描述

You could apply a Style with a DataTrigger to the TextBlock elements to be hidden:您可以将带有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.

相关问题 在绑定到组合框的列表中删除某项时,WPF组合框选择消失 - WPF Combobox selection disappears when the an item is deleted in the list bound to combobox 如何在Xceed WPF工具箱的CheckComboBox中为下拉列表和所选项目显示不同的值? - How to display a different value for dropdown list and selected item in a CheckComboBox in Xceed WPF toolkit? 将ComboBox与Generic List绑定,以便组合框项目的值表示绑定对象 - Binding ComboBox with Generic List so that value of combobox item represents bound object 选择comboBox项目时显示CheckListBox的项目列表 - Display a list of item to CheckListBox when comboBox item is selected WPF组合框中所选项目的模板与组合集合中的项目的模板不同 - Different template for selected item in WPF combobox than items in composite collection 不同选定项目的WPF组合框背景 - WPF Combobox Background for Different Selected item WPF组合框具有不同的所选项目和文本 - WPF Combobox with different selected item and text 组合框选择的项目WPF - Combobox selected item WPF 未选择数据绑定 Combobox 中的初始项目。 即使绑定的 Object 有一个值 - Initial Item in Data bound Combobox is not selected. Even though the Bound Object has a Value WPF ComboBox选定的项目参考一个对象 - WPF ComboBox Selected Item with reference to an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM