为什么我无法粘贴从 WPF FlowDocumentScrollViewer 或 Reader 复制的文本?

发布于 2024-11-15 08:06:50 字数 1563 浏览 2 评论 0原文

上一个问题中,我试图找出如何绑定ObservableCollection 到控件,这样我既可以查看所有字符串,也可以选择所有字符串并从内容控件中复制它们。通过使用以下 XAML,该问题的答案最终让我得到了我想要的外观(以及看似行为)。 (我尝试了 FlowDocumentReader 和 FlowDocumentScrollViewer - 它们的行为相同。)

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

ErrorMessages 是我的 ViewModel 属性,它返回 ObservableCollection。它正确绑定到 ItemsSource,并且 元素绑定到集合中的每个字符串。看起来不错,可以用很长时间。这非常接近,我将我的最后一个问题标记为已回答,但我仍然有一个问题。

我右键单击,会出现一个菜单,其中包含“全选”和“复制”选项。使用全选,确实突出显示所有文本,选择复制不会出现错误,但是当我转到记事本(或Word、TextPad等或RTB)时表单)并尝试粘贴文本,但什么也没有显示。作为 WPF 的新手,我怀疑我做错了什么,但我不知道它是什么。世界上不存在“看起来不好看”的文字吗?

[编辑 - 2011 年 6 月 22 日] 由于其他原因,我已更改代码以通过 ItemsControl 内的 ItemTemplate 使用 TextBlock,如下所示,但我仍然无法复制和粘贴。

<DataTemplate x:Key="StringCollection">
   <TextBlock TextWrapping="Wrap" Text="{Binding}" TextAlignment="Left"/>
</DataTemplate>
<!--... now down in the ItemsControl-->
<ItemsControl ItemsSource="{Binding ReceivedData, Mode=OneWay}"
      ItemTemplate="{StaticResource StringCollection}" />

In a previous question I was trying to find out how to bind an ObservableCollection to a control so I could both see all the strings and select all the strings and copy them from the content control. The answers to that question eventually got me the look (and seemingly the behavior)I wanted by using the following XAML. (I tried both a FlowDocumentReader and FlowDocumentScrollViewer - they behave the same.)

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

ErrorMessages is my ViewModel property that returns an ObservableCollection<string>. It binds properly to the ItemsSource and the <Run> element binds to each string in the collection. Looks good, lasts a long time. This was so close I marked my last question as answered but I still have one problem.

I right click and a menu shows up with the Select All and Copy options. Using Select All, does indeed highlight all the text, selecting Copy issues no errors, but when I go to NotePad (or Word, or TextPad etc. or a RTB on the form) and try to paste the text, nothing ever shows up. As a newcomer to WPF I suspect I'm doing something wrong but I don't know what it is. There's no such thing as "lookless" text is there?

[Edit -June 22 2011]
For other reasons I've changed the code to use a TextBlock via an ItemTemplate inside the ItemsControl as shown below, but I still can't copy and paste.

<DataTemplate x:Key="StringCollection">
   <TextBlock TextWrapping="Wrap" Text="{Binding}" TextAlignment="Left"/>
</DataTemplate>
<!--... now down in the ItemsControl-->
<ItemsControl ItemsSource="{Binding ReceivedData, Mode=OneWay}"
      ItemTemplate="{StaticResource StringCollection}" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲娇萝莉攻 2024-11-22 08:06:50

元素绑定到集合中的每个字符串。

它只应该绑定到当前元素(如果有的话)。

无论如何,如果您只有 ItemsControl,那么您的文档实际上根本不包含任何文本。为什么?因为文档中的任何 UIElement 都会自动包装在 BlockUIContainerInlineUIContainer 且不再被视为文本。

一般来说,内容被复制为 XAML、RTF、UnicodeText 和 UnicodeText。文本(我可以观察这些,但可能还有其他格式),您可以尝试在文档中放置一些 Runs ,它们的文本应该正确复制,并且 Clipboard.GetText() 应该返回它们的内容。

<Run> element binds to each string in the collection.

It only should bind to the current element if anything.

Anyway, your document does in fact not contain any text at all if all you have is the ItemsControl. Why? Because any UIElements inside the document are automatically wrapped in a BlockUIContainer or InlineUIContainer and are no longer considered to be text.

In general the content is copied as XAML, RTF, UnicodeText & Text (i could observe those, but there might be other formats), you can try to place some Runs in your document, their text should be copied properly and Clipboard.GetText() should return their contents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文