How to display a different value for dropdown list values/selected item in a WPF ComboBox?

后端 未结 5 1042
长情又很酷
长情又很酷 2020-11-29 08:22

I have a WPF combobox bound to a list of items with long descriptions.

The type bound to the ComboBox has both short and long description as properties. Currently, I

5条回答
  •  囚心锁ツ
    2020-11-29 08:54

    It doesn't seem to work for me now, but this one does:

    public class ComboBoxItemTemplateSelector : DataTemplateSelector {
      public DataTemplate SelectedTemplate { get; set; }
      public DataTemplate DropDownTemplate { get; set; }
    
      public override DataTemplate SelectTemplate(object item, DependencyObject container) {
        var presenter = (ContentPresenter)container;
        return (presenter.TemplatedParent is ComboBox) ? SelectedTemplate : DropDownTemplate;
      }
    }
    

提交回复
热议问题