为什么我无法粘贴从 WPF FlowDocumentScrollViewer 或 Reader 复制的文本?
在上一个问题中,我试图找出如何绑定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
我右键单击,会出现一个菜单,其中包含“全选”和“复制”选项。使用全选,确实突出显示所有文本,选择复制不会出现错误,但是当我转到记事本(或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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它只应该绑定到当前元素(如果有的话)。
无论如何,如果您只有 ItemsControl,那么您的文档实际上根本不包含任何文本。为什么?因为文档中的任何 UIElement 都会自动包装在
BlockUIContainer
或InlineUIContainer
且不再被视为文本。一般来说,内容被复制为 XAML、RTF、UnicodeText 和 UnicodeText。文本(我可以观察这些,但可能还有其他格式),您可以尝试在文档中放置一些
Runs
,它们的文本应该正确复制,并且Clipboard.GetText() 应该返回它们的内容。
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
orInlineUIContainer
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 andClipboard.GetText()
should return their contents.