从绑定到 ObservableDictionary 的 ComboBox 上的值获取选定字段

Get Selected Field off a value on a ComboBox bound to ObservableDictionary

这很尴尬。

我正在尝试将 ObservableDictionary 绑定到 ComboBox。

我这样做没问题,但我进一步将所选值绑定到 属性,但我没有获得该字段的正确值。

这是我的模特

public class Category
{
    public Int64 CategoryId { get; set; }
    public string CategoryRef { get; set; }
    public string Description { get; set; }
}

这是我的视图模型:

public Dictionary<string, Category> _categoryFields;

public Dictionary<string, Category> CategoryFields
{
    get => _categoryFields;
    set
    {
        _categoryFields = value;
        RaisePropertyChanged("CategoryFields");
    }
}

private string _categorySelected;

public string CategorySelected
{
    get => _categorySelected;
    set
    {
        _categorySelected= value;
        RaisePropertyChanged("CategorySelected");
        EvJobCategoryHasChanged?.Invoke();
    }
}

这是我的看法:

<ComboBoxAdv 
    DisplayMemberPath="Value.Description"                                         
    SelectedValue="{Binding CategorySelected,Mode=TwoWay}"                                         
    SelectedValuePath="{Binding Value.Description}"                                        
    ItemsSource="{Binding CategoryFields}" />

我正在尝试获取用户选择的描述。

这可能是您的问题:

SelectedValuePath="{Binding Value.Description}"

这个属性不需要绑定;它应该是 属性 路径,就像 DisplayMemberPath 一样。将其更改为:

SelectedValuePath="Value.Description"