DisplayMemberPath is not working on ComboBox

匆匆过客 提交于 2020-12-15 03:36:20

问题


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.

Here is my combobox

<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>

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;
        }

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);
        }
    }

回答1:


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.

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}" />


来源:https://stackoverflow.com/questions/64468864/displaymemberpath-is-not-working-on-combobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!