繁体   English   中英

如何在 WPF 中将字典绑定到组合框

[英]How to bind a Dictionary to Combobox in WPF

当我将FoundItems字典绑定到 ComboBox 时,ComboBoxItems 项目变得像[1, FoundItem], [2, FoundItem] ...

但我想将RecordName绑定为显示值、 RecordID or Dictionary's Key作为选定项。 并且, SelectedItemID应该使用TwoWay绑定到 ComboBox 。

我正在努力实现这一目标 2 天。 而且还是没有结果。

public int SelectedItemID { get; set; }
public Dictionary<int, FoundItem> FoundItems { get; set; }


    FoundItems = new Dictionary<int, FoundItem>
    {
        { 1, new FoundItem() { RecordID = 1, RecordName = "Test Name 1" } },
        { 2, new FoundItem() { RecordID = 2, RecordName = "Test Name 2" } },
        { 3, new FoundItem() { RecordID = 3, RecordName = "Test Name 3" } },
        { 4, new FoundItem() { RecordID = 4, RecordName = "Test Name 4" } }
    };

FoundItem 类:

internal class FoundItem
{
    public int RecordID { get; set; }
    public string RecordName { get; set; }
}

尝试这个:

<ComboBox ItemsSource="{Binding FoundItems}" DisplayMemberPath="Value.RecordName"
          SelectedValuePath="Value.RecordID"
          SelectedValue="{Binding SelectedItemID}"/>

暂无
暂无

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

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