none
如何绑定字典 RRS feed

  • 问题

  • XAML中如何绑定字典,字典的Value也是一个列表:

    Dictionary<string, List<string>> FileMap { get; set; }

    有2个列表,一个列表显示Key,另外一个列表显示Value:

    <ListBox x:Name="lbOutMappingList" Grid.Row="0"
            ItemsSource="{Binding FileMap}" DisplayMemberPath="Key">
    </ListBox>
    
    <ListBox x:Name="lbInMappingList" Grid.Row="1"
            ItemsSource="{Binding FileMap}" DisplayMemberPath="Value">
    </ListBox>

    第一个可以正常显示Key,第二只显示了“集合”2个字

    2017年9月29日 6:46

答案

  • 你好,

    如果你想ListBoxItem中显示集合,你应该设置ListBoxItem的数据模板。

    参考如下代码:

     <ListBox x:Name="lbInMappingList" Grid.Row="1" ItemsSource="{Binding FileMap}" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Key}"/>
                            <ItemsControl ItemsSource="{Binding Value}"></ItemsControl>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

    显示value集合的元素你可以选择任何集合控件,例如listbox, combobox等等。

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年10月3日 0:49
    版主