【问题标题】:Bind value to ComboBox from dictionary将值从字典绑定到 ComboBox
【发布时间】:2019-02-25 08:32:50
【问题描述】:

我有一个清单:

public SortedDictionary<string, string> ProjectDictionaryList { get; internal set; }

现在我想将值绑定到 ComboBox:

ComboBox DockPanel.Dock="Right" ItemsSource="{Binding Path=ProjectDictionaryList}" 
         IsSynchronizedWithCurrentItem="True" Style="{StaticResource myCombo}" 
         SelectedItem="{Binding SelectedProject}"

但是如何绑定ProjectDictionaryList.Value

【问题讨论】:

    标签: c# wpf dictionary data-binding combobox


    【解决方案1】:

    设置 DisplayMemberPathSelectedValuePath 并绑定 SelectedValue 以通过其键选择字典条目:

    <ComboBox
        ItemsSource="{Binding ProjectDictionaryList}" 
        DisplayMemberPath="Value"
        SelectedValuePath="Key"
        SelectedValue="{Binding SelectedProject}"/>
    

    【讨论】:

      【解决方案2】:

      您可以使用DisplayMemberPath="" 在组合框中显示值或 如果你不想这样做ProjectDictionaryList.Values可以在{Binding Path=}的帮助下绑定

      【讨论】:

      • 这就够了:DisplayMemberPath="{Binding Path=ProjectDictionaryList.Value}" ?
      • 你已经使用DisplayMemberPath="Value"
      • 如果你不想将整个对象绑定到 ui 你也可以这样做ItemsSource="{Binding Path=ProjectDictionaryList.Value}"
      • Binding Path=ProjectDictionaryList.Value 没有意义。 Dictionary 没有 Value 属性。您可能指的是Values 集合,但那为什么还要使用字典呢?
      • 是的,它应该是Values,我刚刚告诉过有一个这样的选项,我认为它可能对其他场景有帮助
      猜你喜欢
      • 2011-03-08
      • 2014-06-13
      • 2021-10-03
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 2013-05-31
      • 2018-02-09
      相关资源
      最近更新 更多