RichTextBox和插入位置

aza*_*arp 6 wpf richtextbox

这是交易:我有一个RichTextBox控件,它工作正常.问题是有一个"插入当前日期时间"按钮,它将当前日期时间添加/注入RichTextBox.用户可以在插入符指向的任何地方输入日期时间.这涉及复杂的字符串操作和东西.

任何想法如何获得当前的插入位置.每当我得到RichTextBox.CaretPositon时,它似乎指向RichTextBox的开头而不是实际插入符的位置.

更新1:

日期时间按钮单击代码:

 private void DateTimeStampButton_Click(object sender, RoutedEventArgs e)
        {
            //TextRange tr = new TextRange(textBox.Selection.Start, textBox.Selection.End);
            var tr = new TextRange(textBox.Document.ContentStart, textBox.Document.ContentEnd);

            if(tr.Text.Length == 2)
            {
                if(tr.Text == "\r\n")
                {
                    tr.Text = tr.Text.TrimStart(new[] { '\r', '\n' }); 
                }
            }

            textBox.CaretPosition.InsertTextInRun(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":  ");

            DateTimeStampButton.Focusable = false;
        }

 private void SharpRichTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            SetValue(TextProperty, Text);

            var binding = BindingOperations.GetBinding(this, TextProperty);

            if (binding == null) return;

            if (binding.UpdateSourceTrigger == UpdateSourceTrigger.Default || binding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus)
            {
                // if (TextProperty != null) BindingOperations.GetBindingExpression(this, TextProperty).UpdateSource();
            }
        }






public string Text
        {
            get
            {
                var newValue = new TextRange(Document.ContentStart, Document.ContentEnd).Text.RemoveNewLineAndReturn(); 
                return newValue; 
            }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    SetValue(TextProperty, value.RemoveNewLineAndReturn());
                    Document.Blocks.Clear(); 
                    Document.Blocks.Add(new Paragraph(new Run(value))); 
                    OnPropertyChanged("Text"); 
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

更新2:

原来问题是DateTime按钮是Focusable.我把它变得无法集中,它按预期工作.当焦点在RichTextBox上丢失时,它正在重置插入位置.它只发生一次,因为在代码中btn_DateTime被动态设置为Focusable = false.我在XAML中放置了Focusable = false,从一开始就一切正常.

Aus*_*nen 12

我正在使用此代码成功执行您尝试的操作:

private void insertNowButton_Click(object sender, RoutedEventArgs e)
{
    //NOTE:  The caret position does not change.
    richTextBox1.CaretPosition.InsertTextInRun(DateTime.Now.ToString());
}
Run Code Online (Sandbox Code Playgroud)

编辑:解决更新1

private void DateTimeStampButton_Click(object sender, RoutedEventArgs e)
{
    var tr = new TextRange(textBox.Document.ContentStart, textBox.Document.ContentEnd);

    if (tr.Text.Length == 2)
    {
        if (tr.Text == "\r\n")
        {
            tr.Text = tr.Text.TrimStart(new[] { '\r', '\n' });
        }
    }

    /* Changing the text is the only way I can get the date to insert at the beginning */
    tr.Text = "I need a beer at ";

    textBox.CaretPosition.InsertTextInRun(DateTime.Now.ToString());
}
Run Code Online (Sandbox Code Playgroud)

它似乎SetValue正在改变文本,所以根据我的测试,实际更改文本重置插入符号,我会同意你,这SetValue是导致问题...