2

我正在寻找一种FlowDocument在类似标签的控件中呈现短字符串的方法。

在 WPF 中,用户可以将文本输入到RichTextBox. 结果是一个FlowDocument字符串。我正在寻找一种在 a 中呈现该文本的方法Label,其中:

  1. 用户不应该能够编辑或选择(用鼠标)文本。
  2. 不应该有滚动条——就像在普通标签中一样,控件应该扩展以容纳所有文本。
  3. 如果用户在鼠标位于标签上时滚动,则应该滚动的控件是该控件的父级
  4. 控件应尽可能轻巧。

我有以下继承的实现,FlowDocumentScrollViewer但我确信一定有更好的实现(可能继承除 之外的其他控件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(FlowDocumentViewer), 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);
        }
    }
}
4

2 回答 2

0

您是否尝试过设置 IsReadOnly?

    <RichTextBox  IsReadOnly="True"/>
于 2011-07-22T22:25:14.067 回答
0

我认为这个 XAML 满足您的所有要求;让用户控制它应该是微不足道的。只需实现一个设置'属性的FlowDocument依赖属性。RichTextBoxDocument

您在下面看到的大部分内容只是希腊文本,因此您可以在将其粘贴到 Kaxaml 或其他任何内容时看到它的功能;它的核心是一个Grid包含RichTextBox一个透明Border的,这样它就不会捕获鼠标事件或击键。

<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>
于 2011-08-25T18:54:10.467 回答