DataBinding Dictionary Collection到RichTexbox,显示集合的键和值

时间:2013-10-11 08:15:44

标签: c# .net wpf xaml

XAML的MarkUp:

<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窗口中显示字典集合的键或值。是否有可能在它们之间使用适当的格式或空格同时显示两者?有什么建议吗?

1 个答案:

答案 0 :(得分:2)

这是你如何实现这个目标的:

<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>