Reputation: 41
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?
Upvotes: 3
Views: 1233
Reputation: 3025
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.
Upvotes: 1