Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I paste text copied from a WPF FlowDocumentScrollViewer or Reader?

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}" />
like image 253
Tod Avatar asked Jun 14 '11 21:06

Tod


1 Answers

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

like image 199
H.B. Avatar answered Sep 16 '22 12:09

H.B.