简体   繁体   English

如何在Xceed WPF工具箱的CheckComboBox中为下拉列表和所选项目显示不同的值?

[英]How to display a different value for dropdown list and selected item in a CheckComboBox in Xceed WPF toolkit?

I've got a CheckComboBox bound to a list consisting of one simple index and a full description like this: 我有一个CheckComboBox绑定到一个列表,该列表由一个简单的索引和完整的说明组成,如下所示:

code   full_description
1      Bus
2      Car
3      Motobike

Is it possible to show just 1,2,3 for selected items and show full description when dropping down the list? 下拉列表时是否可以仅显示1,2,3并显示完整说明? I found a similar question here . 我在这里发现了类似的问题。 But since CheckComboBox using the difference implementation, I have modified the part of class ComboBoxItemTemplateSelector as follows: 但是由于CheckComboBox使用差异实现,因此我对class ComboBoxItemTemplateSelector的部分进行了如下修改:

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {   
        SelectorItem selectorItem = VisualTreeHelpers.GetVisualParent<SelectorItem>(container);

        if (selectorItem != null)
        {
            return DropDownTemplate;
        }
        return SelectedTemplate;
    }

It works fine with dropdown list, but no luck with selected items. 它在下拉列表中正常工作,但对选定项没有运气。 I tried to dig into the source code of CheckComboBox but got nothing. 我试图深入研究CheckComboBox的源代码,但一无所获。 Hope someone can help me out. 希望有人可以帮助我。 Thanks. 谢谢。

Did you try to just set the ContentTemplate of ItemContainerStyle ? 您是否尝试仅设置ItemContainerStyleContentTemplate

<ComboBox.ItemContainerStyle>
    <!--Not sure what TargetType you should use-->
    <Style TargetType="ComboBoxItem">
        <Setter Property="ContentTemplate">
            <Setter.Value>
                <DataTemplate>
                    <!--Here goes your DropDownTemplate-->
                    <TextBlock Text="{Binding Description}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ComboBox.ItemContainerStyle>

Also, set DisplayMemberPath to your desired property. 另外,将DisplayMemberPath设置为所需的属性。

1.override ToString() in your item class: 1.override ToString()在您的项目类:

 public override string ToString()
 {
    return  full_description;
 }

2.set DisplayMemberPath to code 2.设置DisplayMemberPathcode

<CheckComboBox ItemsSource="{Binding CarList}"  
              DisplayMemberPath="code" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CheckComboBox WPF扩展工具包 - CheckComboBox WPF Extended Toolkit WPF ComboBox 与 object 列表绑定,并且在所选项目中显示的值比在项目列表中的不同 - WPF ComboBox bound with object list and display less different value in selected item than in the item list WPF扩展工具包CheckComboBox不显示项目 - WPF Extended Toolkit CheckComboBox doesn't display items WPF工具包模板中的CheckComboBox自定义 - CheckComboBox in WPF Toolkit Template Customize WPF工具包:CheckComboBox和[Flags]枚举 - WPF Toolkit: CheckComboBox and [Flags] enum WPF Xceed PropertyGrid显示“ Xceed.Wpf.Toolkit.PropertyGrid.Attributes.Item”,而不是实际的DisplayName - WPF Xceed PropertyGrid showing “Xceed.Wpf.Toolkit.PropertyGrid.Attributes.Item” instead of the real DisplayName 检查ComboBox(Xceed WPF工具包),如何从数据库中获取要显示的短名称? - Check ComboBox (Xceed WPF Toolkit), How to get short name to display from database? 工具栏中的扩展WPF工具包CheckComboBox样式 - Extended WPF Toolkit CheckComboBox Style in ToolBar WPF扩展工具包-CheckComboBox SelectedValue排序 - WPF Extended Toolkit - CheckComboBox SelectedValue Ordering Xceed CheckComboBox无法正确显示从XAML硬编码的项目中选择的值 - Xceed CheckComboBox not properly showing selected values from items hardcoded in XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM