Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf RichTextBox并在插入符号位置插入_Wpf_Richtextbox - Fatal编程技术网

Wpf RichTextBox并在插入符号位置插入

Wpf RichTextBox并在插入符号位置插入,wpf,richtextbox,Wpf,Richtextbox,这里是交易:我有一个RichTextBox控件,它工作得很好。问题是有一个按钮“插入当前日期时间”,它将当前日期时间添加/注入RichTextBox。用户可以在插入符号指向的任何位置输入日期时间。这涉及到复杂的字符串操作等 任何关于如何获得当前插入符号位置的想法。每当我得到RichTextBox.caretposition时,它似乎指向RichTextBox的开始,而不是实际插入符号所在的位置 更新1: 日期时间按钮单击代码: private void DateTimeStampButton_

这里是交易:我有一个RichTextBox控件,它工作得很好。问题是有一个按钮“插入当前日期时间”,它将当前日期时间添加/注入RichTextBox。用户可以在插入符号指向的任何位置输入日期时间。这涉及到复杂的字符串操作等

任何关于如何获得当前插入符号位置的想法。每当我得到RichTextBox.caretposition时,它似乎指向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"); 
                }
            }
        }
更新2:


原来问题在于DateTime按钮是可聚焦的。我把它调到不可聚焦的状态,它按预期工作。当RichTextBox上的焦点丢失时,它正在重置插入符号位置。这只发生了一次,因为在代码中btn_DateTime被动态设置为Focusable=false。我在XAML中设置了Focusable=false,从一开始一切都很好

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

private void insertNowButton_Click(object sender, RoutedEventArgs e)
{
    //NOTE:  The caret position does not change.
    richTextBox1.CaretPosition.InsertTextInRun(DateTime.Now.ToString());
}
编辑:寻址更新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());
}

看起来,
SetValue
正在更改文本,因此根据我的测试,实际更改文本会重置插入符号,我同意您的说法,
SetValue
导致了问题…

我使用WPFToolkit.Extended RichTextBox尝试了此解决方案,但它对我无效。 然而,我发现了另一个,并认为这将是很好的张贴在这里,以防其他人可以使用它

我的问题还在于,在我单击了一个按钮(该按钮应该在插入符号位置追加文本)之后,它反而将其添加到RichTextBox的开头

所以我找到的解决方案与这里的类似-

我没有使用CaretPosition,而是使用RichTextBox.Selection.Start.InsertTextInRun(“SomeText”)

它认为选择的开始是插入符号位置,即使没有选择,因此对我来说已经足够好了

我希望有人会觉得这很有用:)

这对我很有用:

private void InsertText(String text, RichTextBox rtb)
{
    rtb.CaretPosition = rtb.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);
    rtb.CaretPosition.InsertTextInRun(text);
}
我在这里找到了代码:


我真希望我知道该告诉你什么。根据文档,RichTextBox的CaretPosition应该是当前位置,或者如果用户没有移动或键入任何内容,则应该是RichTextBox的起始位置。是否有方法从CaretPosition获取RichTextBox输入的字符串的索引号。比如,如果弦是“hellow”,而卡雷特在他下面,那么它应该返回2。太棒了!就是这样。我只有一个小问题。如果我键入“hello datetime is”并按下“insert datetime”按钮。然后它显示如下:2010年8月2日下午2:27 hello datetime是。正如您所看到的,出于某种原因,datetime在文本前面加了前缀。如果我移除它并将光标设置在hello datetime文本的末尾,然后再次按下按钮,那么它工作正常。在我的RichTextBox中,我设置textBox.CaretPosition=Document.ContentEnd;在我的简单应用程序(WPF带有一个RichTextBox、一个TextBox和一个按钮)中,它总是将其放在插入符号闪烁的位置。另一个控件窃取插入符号,它插入插入符号最后出现的位置。我的建议:删除所有使用插入符号的代码,然后重试。谢谢你的建议!我浏览了完整的解决方案,没有找到任何其他关于插入符号的引用。我注意到了另一件有趣的事情。如果我键入“hello”,则插入符号位于o的末尾,如“hello |”,我按下日期按钮,它不会插入该位置。但是如果我在同一个位置单击鼠标,它就可以正常工作。这个问题有点可疑。@azamsharp:没有任何代码发布,这段代码是我能给出的最好建议。在我的简单表单中,我可以键入“当前时间是:”(这会给出带有插入符号的“当前时间是:|”;按下与上述代码链接的按钮;然后得到“当前时间是:2/8/2010 3:00:57 PM”。您需要发布更多代码来清除任何可疑信息。