3

I need a control that is able to select the text displayed inside (read only) but I also need to be able to format the text with Bold and Italics because it's a journal citation. Furthermore, I'd like the control to be able to size according to the text or content(stretch). Here's an example:

Child and Family Behavior Therapy 26.1 (2004).

The closest I can get is a RichTextBox following this example. This TextBlock example is also close, but does not allow for font styling in-line (bold, italics, etc..).

Current RichTextBox solution

    richTextBox1.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
    richTextBox1.Document.PageWidth = 1000;

Why this doesn't work: Journals can have long names and with the addition a description it's easy to reach the end of the Page, causing the content to wrap anyway. Conversely, a short-named journal that has no description still has a scroll bar leading to a ton of white space.

Current TextBlock/TextBox solution

    <TextBox Background="Transparent"
     BorderThickness="0"
     Text="{Binding Text, Mode=OneWay}"
     IsReadOnly="True"
     TextWrapping="NoWrap" />

Why this doesn't work: I either use the default TextBlock and am unable to select the text (The user should be able to copy-paste), or use this TextBox binding and lose the styling functionality.

I'm still pretty new to WPF, and have minimal knowledge on how to edit some controls, such as buttons using OverridesDefaultStyle, ControlTemplate etc.. I just can't seem to find a control that has the three properties I need, or a way a control can be customized to do what I want. Any ideas?

Community
  • 1
  • 1
Joe J
  • 41
  • 1
  • 6
  • "(as of July 2015): VS2015 RC allows wordwrap = false" http://stackoverflow.com/a/1369184/3964654 – wingerse Nov 20 '15 at 21:04
  • If you need no word wrap then why in the world do you have TextWrapping="Wrap" ? – paparazzo Nov 20 '15 at 21:07
  • Thank you Empereur Aiman, I have actually seen this, but What is RC? I'm using VS2015 and I've looked for a "wordwrap" property on all three controls and it's not recognized. – Joe J Nov 20 '15 at 21:08
  • Frisbee, That's an example from a separate question that I was using as reference. If I were to implement it, I would set it to No Wrap. I will change to avoid further confusion, I apologize. – Joe J Nov 20 '15 at 21:09
  • Look the the FlowDocument viewer controls. If you can do it in XAML then you can do inline formatting with TextBox. – paparazzo Nov 20 '15 at 21:12
  • I'm downloading VS2015 Update 1 RC via https://www.visualstudio.com/en-us/news/vs2015-update1-vs.aspx but it's still installing and I'm at the end of my day, so I will respond next week when I get a chance to try it out – Joe J Nov 20 '15 at 21:47
  • @EmpereurAiman Even after updating VS 2015 , I don't understand where to find "wordwrap = false" ... I've searched documents, flowdocuments, viewers, none of them have the property, in XAML or otherwise. I feel dumb, like I'm missing something really obvious, but in any case, thank you both for help. – Joe J Nov 23 '15 at 17:42
  • 1
    I couldn't find it either. Maybe you could clarify with the author of that post. – wingerse Nov 23 '15 at 19:47

1 Answers1

1

I would go for a custom TextBlock. RichTextBox seems overkill.

1) Scroll and sizing

<ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
      <ns:SelectableTextBlock TextWrapping="NoWrap" x:Name="stb" TextSelected="stb_TextSelected"/>
</ScrollViewer>

This ensures the presence of the scroll bar for longer texts and its' absence for shorter ones. See this post.

2) Selection and copy/paste. See this post, and especially this answer. This piece of code on Rextester represents a modification to that example, a custom selectable TextBlock with selection highlighting that goes back and forth.

3) Styling and binding InlineCollection. Various ways of binding your citation model to TextBlock.Inlines, like here, or here.

Community
  • 1
  • 1
andrei.ciprian
  • 2,895
  • 1
  • 19
  • 29
  • This worked pretty well, Thanks! The Rextester example was close to what I needed, though I tweaked it so the text stays selected and doesn't auto-copy on select. – Joe J Nov 23 '15 at 17:38