【问题标题】:How to make the SelectedItem in Combobox(customize DataTemplate) shows only specific property of the item如何使 Combobox(自定义 DataTemplate)中的 SelectedItem 仅显示项目的特定属性
【发布时间】:2021-02-26 15:32:24
【问题描述】:

我有一个显示信息列表的ComboBox

  • NameOfEstablishment
  • BIN
  • Owner
  • BusinessAddress

...看起来像这样:

代码:

<ComboBox x:Name="BploList" 
          Text="{Binding Search}" 
          SelectedItem="{Binding SearchSelected}"
          Grid.Row="0"
          Grid.Column="0" 
          Grid.ColumnSpan="2" 
          FontSize="16" 
          materialDesign:HintAssist.Hint="Search"
          Margin="5 5 65 5" 
          IsEditable="True">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding NameOfEstablishment}" FontSize="12" Foreground="#D8AC6A"
                           TextWrapping="Wrap" MaxWidth="250" />
                <TextBlock Text="{Binding BIN}" FontSize="11" TextWrapping="Wrap" MaxWidth="250" />
                <TextBlock Text="{Binding Owner}" FontSize="11" TextWrapping="Wrap" MaxWidth="250" />
                <TextBlock Text="{Binding BusinessAddress}" FontSize="11" TextWrapping="Wrap" MaxWidth="250" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>

问题

SelectedItem 应该显示NameOfEstablishment,但它却显示为完整对象

关于ComboBox的来源

private BindingList<BPLOSearchModel> _bploList;

        public BindingList<BPLOSearchModel> BploList
        {
            get { return _bploList; }
            set 
            {
                _bploList = value;
                NotifyOfPropertyChange(() => BploList);
            }
        }

BPLOSearchModel 的属性

public class BPLOSearchModel
    {
        public string BIN { get; set; }
        public string NameOfEstablishment { get; set; }
        public string BusinessAddress { get; set; }
        public string Owner { get; set; }
    }

至于 SearchSelected

    private string _searchSelected;

    public string SearchSelected
    {
        get { return _searchSelected; }
        set 
        {
            _searchSelected = value;
            NotifyOfPropertyChange(() => SearchSelected);
        }
    }

【问题讨论】:

  • @MickyD 好的,已编辑。
  • 感谢您的编辑

标签: c# wpf xaml caliburn.micro


【解决方案1】:

这正是SelectedValuePath 的用途。它用于

获取或设置用于获取 SelectedItem 的 SelectedValue 的路径

在你的情况下,设置它像

<ComboBox ... SelectedValuePath="NameOfEstablishment" ... />

请注意,微软也有一个很好的How to

【讨论】:

    猜你喜欢
    • 2016-03-12
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    相关资源
    最近更新 更多