WPF:如何使RichTextBox的样子TextBlock的? [英] WPF: How to make RichTextBox look like TextBlock?

查看:639
本文介绍了WPF:如何使RichTextBox的样子TextBlock的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能让<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.aspx"><$c$c>RichTextBox无保证金,边框,填充等?换句话说,以显示相同的方式内容<一个href="http://msdn.microsoft.com/en-us/library/system.windows.controls.textblock.aspx"><$c$c>TextBlock可以 ?我曾尝试这样的:

How can I make RichTextBox with no Margin, Border, Padding etc. ? In another words to display content in the same way as TextBlock does it ? I have tried this:

<RichTextBox Margin="0" Padding="0" Grid.Row="0" BorderThickness="0" >
    <FlowDocument >
        <Paragraph>LLL</Paragraph>
    </FlowDocument>
</RichTextBox>
<TextBlock>LLL</TextBlock>

但结果产生仍然不是我想要的:

But the result produces is still not what I want:

还有文件内容之前一些空间(和还可能之后,在文档的顶部或底部...)。我怎样才能删除它?

There is still some space before document content (and also maybe after, on the top or bottom of the document...). How can I remove it ?

如果你有兴趣,为什么我需要这样的:我试图使<一个href="http://stackoverflow.com/questions/5796457/create-guitar-chords-editor-in-wpf-from-richtextbox/5797833#5797833">H.B.'s回答我的问题<一href="http://stackoverflow.com/questions/5796457/create-guitar-chords-editor-in-wpf-from-richtextbox">Create在WPF吉他和弦编辑,提供字距的工作,我不希望有字符之间不自然的空间。

If you are interested why I need this: I trying to make H.B.'s answer to my question Create guitar chords editor in WPF to work with kerning and I don't want to have unnatural space between characters.

因此​​,这不是控件模板至少不会只因为以下code会产生相同的结果(对视了一眼上面的图片):

So it is not ControlTemplate at least not only that because following code will produce exactly the same result (as the one on the picture above):

<RichTextBox Margin="0" Padding="0" Grid.Row="0" BorderThickness="0">
    <RichTextBox.Template>
        <ControlTemplate>
            <ScrollViewer Padding="0" Margin="0" x:Name="PART_ContentHost"/>
        </ControlTemplate>
    </RichTextBox.Template>
    <FlowDocument PagePadding="0">
        <Paragraph Padding="0" Margin="0" >LLL</Paragraph>
    </FlowDocument>
</RichTextBox>

和我想这将是问题容易回答... 有趣的现象:时,我有模板集我集 PagePadding =0的FlowDocument 它显示的布局,我想在VisualStudio中设计师 - 直到我运行演示。在演示中是错了......而当我关闭该演示是错误又在设计师。这是VS的一个小bug或者它实际上设置为正确的布局一段时间,但随后有新的变化的值 PagePadding 返回一些错误的价值?

And I thought this will be question easy to answer... Interesting observation: when I have template set and I set PagePadding="0" on FlowDocument it displays layout that I want in the VisualStudio designer - until I run the demo. In the demo it is wrong again... And when I close the demo it is wrong again in the designer. This is a small bug of VS or is it actually set to the right layout for a while but then something changes value of PagePadding back to some wrong value ?

丹尼尔·罗斯的编辑答案也没有为我工作。这是XAML:

Daniel Rose's edited answer is also not working for me. This is XAML:

<FlowDocument PagePadding="{Binding PagePadding}">
    <Paragraph x:Name="paragraph" Padding="0" 
        TextIndent="0"  Margin="0,0,0,0" >hello</Paragraph>
</FlowDocument>

这是在code:

And this is in code:

public static DependencyProperty PagePaddingProperty =
            DependencyProperty.Register("PagePadding", typeof(Thickness),   typeof(EditableTextBlock),
            new PropertyMetadata(new Thickness(0)));

public Thickness PagePadding {
    get { return (Thickness)GetValue(PagePaddingProperty); }
    set { SetValue(PagePaddingProperty, value); }
}

的结果没有改变。空间仍然存在。

增加双向绑定的丹尼尔·罗斯在自己拉斯维加斯编辑它的工作原理建议后。不过我真的不认为这是非常清楚的(有依赖属性,因为我需要保持 PagePadding 0值)。 我认为这是一个黑客 - 错误的解决方法如果有人有更好的解决方案,请分享。

After adding Two-Way binding as Daniel Rose suggested in his las edit it works. Still I don't really think it is very clear (to have dependency property because I need to keep PagePadding at 0 value). I think it is a hack - bug workaround. If somebody has better solution please share it.

显然改变 PagePadding 的FlowDocument 0.5 是一个错误。如果有人有MSDN账号这将是很好,如果他们报告了这个bug。

Obviously "changing PagePadding" of FlowDocument to 0,5 is a bug. If somebody has MSDN account it would be nice if they reported this bug.

推荐答案

我知道这很烦人的地狱。

I know this is annoying as hell.

的RichTextBox 设置这个PagePadding在它的​​ CreateRenderScope(),即当它被连接到可视化树。这时所有的属性通常已经设置,因此,PagePadding被重置。

RichTextBox sets this PagePadding in it's CreateRenderScope(), ie when it gets attached to the visual tree. At this time all properties are usually already set and thus the PagePadding gets reset.

我要告诉你的是一个更一般的如何使用附加属性做到这一点的形式是什么。在我自己的code我这样做通常比较紧,因为我知道一)的FlowDocument不会改变(不必担心注册相同的处理两次)和b)填充不会更改(具有事件处理程序仅仅是((的FlowDocument)S).PagePadding =新的厚度(0.0); 有关既然如此,虽然我会提供,你可以只插一个通用的解决方案

What I'm about to show you is a more general form of how you can do this using an attached property. In my own code I do this usually more tightly because I know that a) the flowdocument does not change (not having to worry about registering the same handler twice) and b) the padding does not change (having the eventhandler just be ((FlowDocument)s).PagePadding = new Thickness(0.0);. For this being SO though I'll provide a general solution that you can just plug in.

        <RichTextBox BorderThickness="0" Margin="0" Padding="0">
            <FlowDocument local:FlowDocumentPagePadding.PagePadding="0">
                <Paragraph>
                    <Run>text</Run>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>


public static class FlowDocumentPagePadding
{
    public static Thickness GetPagePadding(DependencyObject obj)
    {
        return (Thickness)obj.GetValue(PagePaddingProperty);
    }
    public static void SetPagePadding(DependencyObject obj, Thickness value)
    {
        obj.SetValue(PagePaddingProperty, value);
    }
    public static readonly DependencyProperty PagePaddingProperty =
        DependencyProperty.RegisterAttached("PagePadding", typeof(Thickness), typeof(FlowDocumentPagePadding), new UIPropertyMetadata(new Thickness(double.NegativeInfinity),(o, args) =>
            {
                var fd = o as FlowDocument;
                if (fd == null) return;
                var dpd = DependencyPropertyDescriptor.FromProperty(FlowDocument.PagePaddingProperty, typeof(FlowDocument));
                dpd.RemoveValueChanged(fd, PaddingChanged);
                fd.PagePadding = (Thickness) args.NewValue;
                dpd.AddValueChanged(fd, PaddingChanged);
            }));
    public static void PaddingChanged(object s, EventArgs e)
    {
        ((FlowDocument)s).PagePadding = GetPagePadding((DependencyObject)s);
    }
}

原始出处code解说:

RichTextBox.CreateRenderScope()开发商的原始来源包括此评论:

original sourcecode commentary:

In the original source of RichTextBox.CreateRenderScope() the developers included this comment:

// Set a margin so that the BiDi Or Italic caret has room to render at the edges of content.
// Otherwise, anti-aliasing or italic causes the caret to be partially clipped.
renderScope.Document.PagePadding = new Thickness(CaretElement.CaretPaddingWidth, 0, CaretElement.CaretPaddingWidth, 0);

的bug报告

<一个href="https://connect.microsoft.com/WPF/feedback/details/642175/flowdocument-pagepadding-value-is-ignored-in-markup">here是在Microsoft Connect上

这篇关于WPF:如何使RichTextBox的样子TextBlock的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆