从组合框中的字典加载数据

时间:2013-08-30 10:32:56

标签: c# wpf dictionary combobox

您好我在WPF中有组合框

<ComboBox Name="mcombo" SelectedValuePath="Key" >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=dictionary}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

也落后于我

Dictionary<string, string> dictionary = new Dictionary<string, string>();
        dictionary.Add("val", "valvalval"); 

我忘记了什么?在组合框中,没有数据!

1 个答案:

答案 0 :(得分:0)

首先,字典应该是属性不可变的。

然后你应该将Dictionary绑定到ItemsSource

<ComboBox Name="mcombo" SelectedValuePath="Key" ItemsSource="{Binding Dictionary}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Value}" /> /Dont bind dictionary here. makes no sense
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

希望这会有所帮助..

由于

相关问题