【问题标题】:How to bind textblock to combobox with nested dictionary as source如何将文本块绑定到以嵌套字典为源的组合框
【发布时间】:2017-04-26 13:32:20
【问题描述】:

我有两个ComboBox。第一个的来源是dictionary,字符串作为键,对象作为值。选择一个项目后,第二个ComboBox 将填充来自所选项目的单独dictionary 的键。 When an item from the second ComboBox is selected, the TextBlock should show the value of the key that was chosen in the second ComboBox.但是,文本块总是显示为空。我已确保该值中确实包含实际数据,这让我相信这是一个绑定问题。

这是我的 ViewModel 的相关部分:

GPHDTModel gphdtModel = new GPHDTModel();

private Dictionary<string, object> models = new Dictionary<string, object>();
public Dictionary<string, object> Models
{
    get
    {
        return models;
    }
}

public MainWindowViewModel()
{
    gphdtModel.MessageID = "3";
    models.Add("GPHDT", gphdtModel);
}

接下来是 GPHDTModel:

private Dictionary<string, string> _fields = new Dictionary<string, string>();
public Dictionary<string, string> Fields
{
    get
    {
        return _fields;
    }
}

public GPHDTModel()
{
    _fields.Add("MessageID", MessageID);
}

private string _messageID;
public string MessageID
{
    get { return _messageID; }
    set { _messageID = value; OnPropertyChanged("MessageID"); }
}

终于看到了:

<ItemsControl ItemsSource="{Binding DataModelCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <ComboBox x:Name="NMEAlist"
                          DisplayMemberPath="Key"
                          ItemsSource="{Binding Path=DataContext.Models,
                                                RelativeSource={RelativeSource Mode=FindAncestor,
                                                                               AncestorType={x:Type ItemsControl}}}"
                          SelectedValuePath="Value" />
                <ComboBox x:Name="ModelList"
                          DisplayMemberPath="Key"
                          ItemsSource="{Binding SelectedItem.Value.Fields,
                                                ElementName=NMEAlist}"
                          SelectedValuePath="Value" />
                <TextBlock Text="{Binding Value,
                                          ElementName=ModelList}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

编辑:在TextBlock 的绑定中使用转换器进行调试,它显示了正确的键,在本例中为“MessageID”,但键的值为空,而它应该为“3”。

正如下面@mm8 所说,当像这样绑定文本块时:Text="{Binding SelectedItem.Key, ElementName=ModelList}"“MessageID”出现在文本块中。因此使用SelectedItem.Value 绑定是正确的,但值设置不正确。

【问题讨论】:

    标签: c# wpf dictionary data-binding combobox


    【解决方案1】:

    尝试绑定ComboBoxSelectedItem属性的Value属性:

    <TextBlock Text="{Binding SelectedItem.Value, ElementName=ModelList}" />
    

    【讨论】:

    • 试过这个,不幸的是仍然没有。我在文本块中添加了一个转换器,以便我可以调试该值,并且该值显示为空。所以由于某种原因它没有获得价值。
    • 你看到ModelList ComboBox中的项目了吗?
    • 是的。第一个组合框显示 GPHDT,当我选择它时,第二个组合框显示“MessageID”。但是,在选择该选项后,文本块将保持为空,而不是显示 MessageID 属性等于的“3”。
    • 如果你像这样绑定它的 Text 属性,TextBlock/converter 会说什么?:{Binding SelectedItem, ElementName=ModelList}
    • 嗯,它显示的是值(即字典),键是“MessageID”,但值为空。也许我的字典搞砸了。还是我对第二个组合框的绑定?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    • 2016-09-17
    • 2016-02-18
    • 2016-08-28
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多