DisplayMemberPath在ComboBox上不起作用 [英] DisplayMemberPath is not working on ComboBox

查看:72
本文介绍了DisplayMemberPath在ComboBox上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,该组合框绑定到名为"BlockDetails"的属性,在视图模型中.当我展开组合框时,可以看到其中的项目.但是问题是它没有选择/显示项目.在顶部,当我设置 SelectedValue ="{Binding BlockName,Mode = TwoWay}" 时,在输出窗口中给出了绑定路径错误,提示'Error:BindingExpression path error:'BlockName在"Presentation.InstrumentUI.ViewsLoggedIn.ServiceUtilityMethodsView"上找不到"属性.BindingExpression:路径="BlockName"数据项="Presentation.InstrumentUI.ViewsLoggedIn.ServiceUtilityMethodsView";目标元素是'Windows.UI.Xaml.Controls.ComboBox'(Name ='null');target属性是"SelectedValue"(类型为"Object").我不明白为什么它会在View中而不是在模型中进行搜索.请帮忙.

I have a combobox which is bound to a property called "BlockDetails" in the viewmodel. When I expand the combobox I can see the items inside it. But the problem is it doesn't select/display the item. On top when I set SelectedValue="{Binding BlockName,Mode=TwoWay}", in the output window it gives a binding path error saying 'Error: BindingExpression path error: 'BlockName' property not found on 'Presentation.InstrumentUI.ViewsLoggedIn.ServiceUtilityMethodsView'. BindingExpression: Path='BlockName' DataItem='Presentation.InstrumentUI.ViewsLoggedIn.ServiceUtilityMethodsView'; target element is 'Windows.UI.Xaml.Controls.ComboBox' (Name='null'); target property is 'SelectedValue' (type 'Object')'. I don't understand why is it going and searcing in the View instead of the model. Please help.

这是我的组合框

<ComboBox uwpControls:DockPanel.Dock="Right"
                              Margin="16,0,0,0"
                              Style="{StaticResource ComboBoxStyleForm}"
                              ItemsSource="{x:Bind ViewModel.BlockDetails,Mode=TwoWay}"
                              DisplayMemberPath="BlockName"
                              SelectedValuePath="BlockName"
                              SelectedValue="{Binding BlockName,Mode=TwoWay}"></ComboBox>

在后面的代码中,我具有如下所示的ViewModel,组合框的项目源已正确绑定

In the code behind I have the ViewModel as follows, the item source for the Combobox is bound correctly

 public IServiceUtilityMethodsViewModel ViewModel { get; }
        public ServiceUtilityMethodsView()
        {
            InitializeComponent();
            ViewModel = LifetimeScope.Resolve<IServiceUtilityMethodsViewModel>();
            DataContext = this;
        }

这是viewmodel属性.

Here is the viewmodel property.

 public List<VmServiceMethodBlockDefinition> BlockDetails
        {
            get => _blockDetails;
            set => Set(ref _blockDetails, value);
        }

在我的模型中,该类的声明如下,

In my model the class is declared as follows,

public class VmServiceMethodBlockDefinition : BindableBaseThreadSafe
    {
        private string _blockName;
        public string BlockName
        {
            get => _blockName;
            set => Set(ref _blockName, value);
        }

        private List<VmServiceMethodBlockParameters> _blockParameters;
        public List<VmServiceMethodBlockParameters> BlockParameters
        {
            get => _blockParameters;
            set => Set(ref _blockParameters, value);
        }
    }

推荐答案

我认为您或者混淆了 SelectedValue 属性,或者错过了一些细节.您有一个对象或数据类型,它是 ComboBox 的一项.
您具有 ComboBox.DisplayMemberPath 来选择此数据类型的属性,该属性应由下拉列表中的 ComboBox 显示.
最后,您可以使用 ComboBox.SelectedValuePath 将此数据类型的属性选择为 ComboBox.SelectedValue .如果您对实际数据项不感兴趣,而对该项的特定值感兴趣,则使用 ComboBox.SelectedValue 而不是 ComboBox.SelectedItem .这两个属性的作用均相同,即显示当前选定的项目.

I think you either confused the SelectedValue property or missed to post some details.
You have an object or data type that is an item of the ComboBox.
You have ComboBox.DisplayMemberPath to select the property of this data type, which should be displayed by the ComboBox in the drop down.
Finally you have ComboBox.SelectedValuePath to select a property of this data type to be the ComboBox.SelectedValue. You use ComboBox.SelectedValue instead of ComboBox.SelectedItem, if you are not interested of the actual data item, but a specific value of this item. Both properties serve the same purpose of exposing the currently selected item.

您通常将此 ComboBox.SelectedValue 绑定到公开源集合(在您的情况下为 BlockDetails 集合)的同一视图模型(数据上下文)上.您的视图模型应具有属性 SelectedBlockName .然后使用 x:Bind ComboBox.SelectedValue 绑定到此属性:

You typically bind this ComboBox.SelectedValue to the same view model (data context) that exposes the source collection, in your case the BlockDetails collection. Your view model should have a property SelectedBlockName. Then use x:Bind to bind the ComboBox.SelectedValue to this property:

IServiceUtilityMethodsViewModel

public List<VmServiceMethodBlockDefinition> BlockDetails
{
  get => _blockDetails;
  set => Set(ref _blockDetails, value);
}

private string _selectedBlockName
public string SelectedBlockName
{
  get => _selectedBlockName;
  set => Set(ref _selectedBlockName, value);
}

XAML

<ComboBox ItemsSource="{x:Bind ViewModel.BlockDetails, Mode=OneTime}"
          DisplayMemberPath="BlockName"
          SelectedValuePath="BlockName"
          SelectedValue="{x:Bind ViewModel.SelectedBlockName, Mode=TwoWay}" />

这篇关于DisplayMemberPath在ComboBox上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆