如何显示ObservableCollection< string>在用户控件中 [英] How to Display ObservableCollection<string> in a UserControl

查看:150
本文介绍了如何显示ObservableCollection< string>在用户控件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的新手,我发现了一些类似的问题,但是还不太清楚最后一部分.我有一个带有ObservableCollection的ViewModel,其中包含错误消息.我想在表单上显示这些内容,并允许用户选择并复制全部或部分消息. (过去在WinForm应用程序中,我为此使用了RichTextBox,但是我不知道如何在WPF中将其绑定到该集合.)

I'm new to WPF and I've found some similar questions but can't quite figure out the last part. I have a ViewModel with an ObservableCollection that contains error messages. I want to display these on the form AND allow the user to select and copy all or part of the messages. (In the past in WinForm apps I used a RichTextBox for this, but I can't figure out how to bind to one to the collection in WPF.)

使用以下xaml获得了我的外观,但是没有像RichTextBox那样可以选择和复制的内置方法.有谁知道我应该使用哪个控件,或者是否有办法启用选择/复制所有TextBlocks的内容,或将其绑定到RichTextBox的方法?

I got the look I was after with the following xaml, but there is no built-in way to select and copy like I could with a RichTextBox. Does anyone know which control I should use or if there is way to enable selecting/copying the contents of all the TextBlocks, or a way to bind this to a RichTextBox?

    <Grid Margin="6">
    <ScrollViewer VerticalScrollBarVisibility="Auto"  Height="40" Grid.Column="0" Margin="6">
        <ItemsControl ItemsSource="{Binding ErrorMessages}" >            
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                     <TextBlock Text="{Binding Mode=OneWay}" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
</Grid>

@Andrey Shvydky-此评论不适合. 我花了一段时间才弄清楚正确的语法(特别是/,东西),但最终我得到了如下所示的Flow Document语法.它在表格上看起来正确,起初似乎支持全选/复制.但是当我在全选/复制之后粘贴时,什么也没有显示.有人知道为什么吗?

@Andrey Shvydky - This wouldn't fit in a comment. It took me a while to figure out the proper syntax (especially the /, thing) but eventually I ended up with the Flow Document syntax shown below. It looks correct on the form and at first seems to support select all/copy. But when I paste after a select all/copy nothing ever shows up. Anyone know why?

 <Grid Margin="6">
    <FlowDocumentScrollViewer>
        <FlowDocument >
            <Paragraph>
                <ItemsControl ItemsSource="{Binding ErrorMessages, Mode=OneWay}" />
                <Run Text="{Binding /, Mode=OneWay}" />
            </Paragraph>
        </FlowDocument>
    </FlowDocumentScrollViewer>
</Grid>

推荐答案

对于生成FlowDocument并在流文档概述.

May be usefull to generate FlowDocument and show this document in FlowDocumentReader. Try to start from this article: Flow Document Overview.

生成示例:

    void ShowErrors(FlowDocumentReader reader, Exception[] errors) {
        FlowDocument doc = new FlowDocument();
        foreach (var e in errors) {
            doc.Blocks.Add(new Paragraph(new Run(e.GetType().Name)) {
                Style = (Style)this.FindResource("header")
            });
            doc.Blocks.Add(new Paragraph(new Run(e.Message)) {
                Style = (Style)this.FindResource("text")
            });
        }
        reader.Document = doc;
    }

在此示例中,我为flowdocument中的文本添加了一些样式.请看一下XAML:

In this example I have added some styles for text in flowdocument. PLease look at XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="header" TargetType="{x:Type Paragraph}">
        <Setter Property="FontWeight" Value="Bold"/>
    </Style>
    <Style x:Key="text" TargetType="{x:Type Paragraph}">
        <Setter Property="Margin" Value="30, 0, 0, 0"/>
    </Style>
</Window.Resources>
<FlowDocumentReader Name="reader">
</FlowDocumentReader>

结果:

这篇关于如何显示ObservableCollection&lt; string&gt;在用户控件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆