0

I have a ComboBox, and I have bound a Dictionary to it. Here is the code:

<ComboBox Height="24" Name="comboBoxQuery" Width="300" ItemsSource="{Binding QueryNames}" SelectedItem="{Binding SelectedQueryNames}" SelectedValuePath="Key" DisplayMemberPath="Value" Visibility="{Binding Path=ComboVisibility, Converter={StaticResource BoolToVis}}" />

Now, when I try to fetch the selected value, I get a string with key and value together. Ex: [1, "ABC"]

my view model code:

    public Dictionary<int, string> QueryNames 
    { 
        get 
        { 
            return m_ReadOnlyQueryNames; 
        }
        set
        {
            m_queryNames = value;
            OnPropertyChanged("QueryNames");
        }
    }

    private string m_SelectedQueryNames;        

    public string SelectedQueryNames
    {
        get 
        {
            return m_SelectedQueryNames;
        }            
        set
        {
            if (m_SelectedQueryModule != value) <!-- value returns [1, "abc"]-->
            {
                m_SelectedQueryModule = value;
                OnPropertyChanged("SelectedQueryNames");
            }
        }
    }
4

1 回答 1

0

如果您正在使用SelectedItem,您的属性将获得整个对象(在您的情况下,属性将获得带有键和值的字典元素)。如果您只想获取键或值,请尝试使用SelectedValue. 但这取决于您如何设置SelectedValuePath

问候,

于 2013-06-14T21:41:00.803 回答