【问题标题】:Combobox binded to Dictonary and retrieving the associated dictionary key组合框绑定到字典并检索关联的字典键
【发布时间】:2013-06-14 21:23:21
【问题描述】:

我有一个ComboBox,我绑定了一个Dictionary。代码如下:

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

现在,当我尝试获取所选值时,我会得到一个包含键和值的字符串。 例如:[1, "ABC"]

我的视图模型代码:

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

【问题讨论】:

    标签: wpf xaml data-binding dictionary combobox


    【解决方案1】:

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

    问候,

    【讨论】:

      猜你喜欢
      • 2018-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多