数据绑定字典集合到 RichTexbox,显示集合的键和值
本文关键字:集合 显示 键和值 RichTexbox 字典 数据绑定 | 更新日期: 2023-09-27 18:34:57
MarkUp for XAML:
<RichTextBox HorizontalAlignment="Left" Height="120" Margin="359,554,0,0" VerticalAlignment="Top" Width="306" Name="duplicateCompXMLVarRichTextBox">
<FlowDocument>
<Paragraph>
<ItemsControl ItemsSource="{Binding Value}"/>
<!-- <ItemsControl ItemsSource="{Binding Key}"/> -->
</Paragraph>
</FlowDocument>
</RichTextBox>
代码隐藏:
Dictionary<string, List<string>> componentXMLDuplicates = new Dictionary<string, List<string>>();
componentXMLDuplicates= Helper.PopulateCollectionWithData();
duplicateCompXMLVarRichTextBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
duplicateCompXMLVarRichTextBox.DataContext = componentXMLDuplicates;
这里的问题是我只能在 WPF 窗口中显示字典集合的键或值。是否有可能使用适当的格式或它们之间的空格同时显示两者?有什么建议吗?
这是实现此目的的方法:
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Key}"/>
<ItemsControl ItemsSource="{Binding Value}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>