简体   繁体   English

RichTextBox自定义控件

[英]RichTextBox Custom Control

I'm trying to create a new custom control, which inherits from RichTextBox. 我正在尝试创建一个新的自定义控件,它继承自RichTextBox。 The reason for this is to add custom buffering to the control (eg only append text every x milliseconds and/or buffer.Length > x). 这样做的原因是为控件添加自定义缓冲(例如,每x毫秒和/或buffer.Length> x只附加文本)。

I've managed to create the control and add it to the xaml Window, however it doesn't seem to actually operate as a RichTextBox properly - text doesn't display after being appended, and the cursor doesn't change icon when hovered over the control . 我已经设法创建控件并将其添加到xaml窗口, 但它似乎并没有正确地作为RichTextBox运行 - 文本在追加后不显示,并且光标在悬停时不会改变图标控制

It seems to be fairly simple code, so I'm not sure where I'm going wrong. 它似乎是相当简单的代码,所以我不确定我哪里出错了。

CBufferedTextBox.cs: CBufferedTextBox.cs:

public class CBufferedTextBox : RichTextBox
{
    const int MAX_LENGTH = 2048;
    const int TIMER_LENGTH = 1000;

    DispatcherTimer m_timer = new DispatcherTimer();

    DispatcherTimer Timer
    {
        get { return m_timer; }
    }

    StringBuilder m_currentText = new StringBuilder();

    StringBuilder CurrentText
    {
        get { return m_currentText; }
    }


    static CBufferedTextBox()
    {
        DefaultStyleKeyProperty.OverrideMetadata( typeof( CBufferedTextBox ), new FrameworkPropertyMetadata( typeof( CBufferedTextBox ) ) );
    }


    public CBufferedTextBox()
    {
        Loaded += CBufferedTextBox_Loaded;
    }

    public CBufferedTextBox( FlowDocument document )
        : base( document )
    {
    }


    public new void AppendText( string strText )
    {
        CurrentText.Append( strText );

        if( !strText.EndsWith( Environment.NewLine ) )
        {
            CurrentText.AppendLine();
        }

        if( CurrentText.Length > MAX_LENGTH )
        {
            Flush();
        }
    }

    void CBufferedTextBox_Loaded( object sender, RoutedEventArgs e )
    {
        Timer.Interval = new TimeSpan( TIMER_LENGTH );
        Timer.Tick += new EventHandler( Timer_Tick );
        Timer.Start();
    }

    void Timer_Tick( object sender, EventArgs e )
    {
        Flush();
    }

    void Flush()
    {
        Timer.Stop();
        this.BeginInvokeIfRequired( o =>
        {
            if( CurrentText.Length > 0 )
            {
                base.AppendText( CurrentText.ToString() );

                // Clear
                CurrentText.Remove( 0, CurrentText.Length );

                ScrollToEnd();
            }

            Timer.Start();
        } );
    }
}

Generic.xaml: Generic.xaml:

<Style TargetType="{x:Type local:CBufferedTextBox}" BasedOn="{StaticResource {x:Type RichTextBox}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CBufferedTextBox}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Thanks, Rich 谢谢,Rich

Well of course not, you completely have overriden the Style and especially the ControlTemplate . 当然不是,你完全覆盖了Style,尤其是ControlTemplate Your Control just consists of a Border, thats all. 你的控制只是一个边界,就是所有。 No text input, no text display no nothing. 没有文字输入,没有文字显示没有任何东西。 You need to implement at least the bare basics in your template, to let your CBufferedTextBox behave like you expect. 您需要在模板中至少实现基本的基础知识,以使您的CBufferedTextBox像您期望的那样运行。

I also want to point out, that your new void AppendText is pretty dangerous and might not do what you expect. 我还想指出,你的new void AppendText非常危险,可能无法达到你的预期。 In your Flush method you call the AppendText of the RichtText box not yours. 在您的Flush方法中,您调用RichtText框的AppendText而不是您的。 Also new is not the same as override. 新的与覆盖不同。 The RichTextBox will never call your method internally, even if it is of your new type CBufferedTextBox. RichTextBox永远不会在内部调用您的方法,即使它是您的新类型CBufferedTextBox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM