简体   繁体   English

SelectedItem 不会绑定到属性

[英]SelectedItem won't bind to property

I have the following ComboBox:我有以下组合框:

    <ComboBox
      ItemsSource="{  Binding RclTypes }"
      DisplayMemberPath = "Key" SelectedValuePath = "Value"
      SelectedItem="{ Binding RclTypeSelected, Mode=TwoWay }"/>

My associated ViewModel file:我关联的 ViewModel 文件:

    private Dictionary<string, ProjectDto> _rclTypes;

    public Dictionary<string, ProjectDto> RclTypes
    {
        get => _rclTypes;
        set => SetProperty(ref _rclTypes, value);
    }

    private ProjectDto _rclTypeSelected;

    public ProjectDto RclTypeSelected
    {
        get => _rclTypeSelected;
        set => SetProperty(ref _rclTypeSelected, value);
    }
    public myConstructor()
    {
        PopulateRclTypes();
    }

    public void PopulateRclTypes()
    {
       _rclTypes = new Dictionary<string, ProjectDto>();

            foreach (ProjectDto runType in Enum.GetValues(typeof(ProjectDto)))
            {
                string firstThreeLettersOfRunTypeName = runType.ToString().Substring(0, 3);

                if (firstThreeLettersOfRunTypeName == "Rcl" && runType.ToString().Length > 3)
                {
                    string displayRunTypeName = runType.ToString().Substring(3, runType.ToString().Length - 3);
                    _rclTypes.Add(displayRunTypeName, runType);
                }
             }
      }
        

The ComboBox dropdown gets populated correctly, but RclTypeSelected property does not get filled with the right object and is always null. ComboBox 下拉列表被正确填充,但 RclTypeSelected 属性没有填充正确的对象并且始终为空。 Why and how can I correct that?为什么以及如何纠正?

SelectedValuePath is meant to be used in conjunction with SelectedValue , not SelectedItem : SelectedValuePath旨在与SelectedValue结合使用,而不是SelectedItem

<ComboBox ItemsSource="{Binding RclTypes}"
          DisplayMemberPath="Key"
          SelectedValuePath="Value"
          SelectedValue="{Binding RclTypeSelected}"/>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM