How to display short read only FlowDocument in a label-like control
I am looking for a way to present short FlowDocument
strings in a label-like control.
In WPF the user can input text into a RichTextBox
. The result is a FlowDocument
string.
I'm looking for a way to present that text in a Label
, in which:
- The user should not be able to edit or select (with the mouse) the text.
- There should be no scroll bars - like in a normal label the control should expand to accommodate all the text.
- If the user scrolls while the mouse is on the label, the control that should scroll is the parent of the control
- The control should be as lightweight as possible.
I have the following implementation that inherits FlowDocumentScrollViewer
but I am sure there must be a better implementation (possibly inheriting other control than FlowDocumentScrollViewer
).
public class FlowDocumentViewer : FlowDocumentScrollViewer
{
public FlowDocumentViewer()
{
this.SetValue(ScrollViewer.CanContentScrollProperty, false);
this.SetValue(ScrollViewer.VerticalScrollBarVisibilityProperty, ScrollBarVisibility.Hidden);
this.Padding = new Thickness(-17);
this.Document = new FlowDocument();
}
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
e.Handled = false;
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(FlowDo开发者_高级运维cumentViewer), new UIPropertyMetadata(string.Empty, TextChangedHandler));
private static void TextChangedHandler(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue.Equals(string.Empty))
return;
FlowDocumentViewer fdv = (FlowDocumentViewer)d;
fdv.Document.Blocks.Clear();
using (MemoryStream stream = new MemoryStream(UTF8Encoding.UTF8.GetBytes(e.NewValue.ToString())))
{
Section content = XamlReader.Load(stream) as Section;
fdv.Document.Blocks.Add(content);
}
}
}
Have you tried setting IsReadOnly?
<RichTextBox IsReadOnly="True"/>
I think this XAML meets all of your requirements; it should be trivial to make a user control out of it. Just implement a FlowDocument
dependency property that sets the RichTextBox
's Document
property.
Most of what you see below is just greeked text so that you can see how it functions when you paste it into Kaxaml or whatever; the core of it is a Grid
containing a RichTextBox
with a transparent Border
over it so that it doesn't capture mouse events or keystrokes.
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel Margin="100">
<ScrollViewer>
<StackPanel>
<Grid>
<RichTextBox Background="Coral">
<FlowDocument>
<Paragraph>
Gluten-free pariatur exercitation laboris, salvia nisi excepteur. Elit quis PBR, jean shorts DIY excepteur tofu retro. Nulla art party farm-to-table, banh mi labore wes anderson marfa Austin portland carles tattooed 8-bit. Skateboard farm-to-table sed, lomo proident iphone mustache. Keffiyeh magna freegan mollit. Seitan viral consequat elit deserunt, occaecat vero tempor. Terry richardson esse mollit, anim chambray DIY squid. </Paragraph>
</FlowDocument>
</RichTextBox>
<Border Background="Transparent"/>
</Grid>
<Grid>
<RichTextBox Background="AliceBlue">
<FlowDocument>
<Paragraph>
Marfa locavore duis, chambray homo irure culpa et high life skateboard. Readymade sartorial odio deserunt. Dolore placeat scenester reprehenderit tattooed nisi. DIY fugiat tempor raw denim. Incididunt sapiente echo park ut yr, deserunt non williamsburg quis. Pitchfork nihil nisi etsy next level elit minim eu, id twee vero exercitation wes anderson. Ullamco beard delectus, before they sold out homo aliquip craft beer esse cillum mlkshk. </Paragraph>
</FlowDocument>
</RichTextBox>
<Border Background="Transparent"/>
</Grid>
<Grid>
<RichTextBox Background="Goldenrod">
<FlowDocument>
<Paragraph>
Laborum cliche quinoa odio nostrud Austin, dolor 3 wolf moon craft beer brunch ex vice. Excepteur ullamco fugiat, shoreditch assumenda squid sapiente craft beer viral vice non incididunt tempor. Ullamco gluten-free veniam, elit fugiat sustainable thundercats wolf fap Austin id nihil viral. Sartorial photo booth Austin, pitchfork labore PBR nisi cardigan dolore. Seitan dolor letterpress, banksy organic biodiesel tattooed aliqua. Letterpress ea 3 wolf moon, cosby sweater williamsburg ethical portland reprehenderit wayfarers nostrud beard laboris. Blog ethical trust fund, quinoa vegan skateboard sed art party messenger bag biodiesel do.
</Paragraph>
</FlowDocument>
</RichTextBox>
<Border Background="Transparent"/>
</Grid>
<Grid>
<RichTextBox Background="AntiqueWhite">
<FlowDocument>
<Paragraph>
Assumenda ad aute cred est. Beard elit fugiat brunch, proident nulla 8-bit. Cardigan sapiente 8-bit tempor put a bird on it duis lo-fi. VHS before they sold out commodo, occaecat raw denim artisan dolor photo booth ex gentrify proident readymade ad. Artisan duis thundercats ex, 8-bit ut williamsburg portland seitan cred vinyl brooklyn. Aute deserunt beard cliche, you probably haven't heard of them commodo artisan tumblr irony put a bird on it VHS excepteur american apparel. Voluptate PBR artisan ut banksy, nostrud organic vero fap anim american apparel trust fund do exercitation. </Paragraph>
</FlowDocument>
</RichTextBox>
<Border Background="Transparent"/>
</Grid>
</StackPanel>
</ScrollViewer>
<TextBlock/>
</DockPanel>
</Page>
精彩评论