当 RichTextBox 刚加载/为空时,WPF EditingCommands 不起作用? [英] WPF EditingCommands is not working when RichTextBox is just load/empty?

查看:39
本文介绍了当 RichTextBox 刚加载/为空时,WPF EditingCommands 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的代码示例:

Here is a very simple code example:

<DockPanel>
    <ToolBar DockPanel.Dock="Top" IsTabStop="False">
         <ToggleButton MinWidth="40"  Command="EditingCommands.ToggleBold" CommandTarget="{Binding ElementName=XAMLRichBox}" TextBlock.FontWeight="Bold" IsTabStop="False">B</ToggleButton>
    </ToolBar>
    <RichTextBox x:Name="XAMLRichBox" SpellCheck.IsEnabled="True" MinHeight="100"/>
</DockPanel>

当我运行它时,在 RichTextBox 中输入一些东西后,我可以使用 ToggleButton 来获得 BOLD 效果,一切都是美好的.

when I run it, after typing something into the RichTextBox, I can use the ToggleButton to get the BOLD effect, and everything is fine.

但是如果我在将任何内容输入到 RichTextBox 之前单击 ToggleButton(无论 RichTextBox 是否获得焦点),尽管 ToggleButton 变成了 Checked,我的 RichTextBox 仍然使用正常样式(不是 BOLD),直到我点击 ToggleButton再次.这是一个错误吗?我怎样才能四处走动?谢谢!

But if I click ToggleButton before typing in anything into RichTextBox (no matter RichTextBox get focus or not), although ToggleButton became Checked, my RichTextBox still using the normal style (not BOLD) until I click ToggleButton again. Is this a bug? how can I get around? Thanks!

推荐答案

我找到了一个半解决方案,我想我会分享,因为网络上的任何地方都没有回答这个问题,而且我认为很多人都遇到了这个问题.

I found a semi-solution and I thought I would share since this problem is not answered anywhere on the web and I think many people are having issues with it.

我在构造函数中设置了一个变量 NewInput.当 RichTextBox 中的第一个输入被触发时,我将应用我需要的所有格式并将其传递给控件.

I set a Variable NewInput in the constructor. When the first input in the richTextBox will be fired, I'll just apply every formating I need to it and pass it to the control.

private bool NewInput;
private void richTxt_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (NewInput)
    {
        richTxt.BeginChange();
        TextPointer startPosition = richTxt.Selection.Start;
        Run r = new Run(e.Text, startPosition);
        if (IsSelectionBold)
        {
            r.FontWeight = FontWeights.Bold;
        }
        if (IsSelectionItalic)
        {
            r.FontStyle = FontStyles.Italic;
        }
        if (IsSelectionUnderlined)
        {
            r.TextDecorations = TextDecorations.Underline;
        }
        r.FontSize = double.Parse(SelectedFontHeight);
        r.FontFamily = new FontFamily(SelectedFont);

        richTxt.EndChange();


        NewInput = false;
        e.Handled = true;
        richTxt.CaretPosition = richTxt.CaretPosition.GetPositionAtOffset(1);
    }
}

然后我在正确的位置更换了carret.这样,即使 RichTextBox 中没有任何内容,格式也会保留.

I then replace the carret at the right place. Like this, the formating is kept even if there is nothing in the RichTextBox.

我相信有一天它会帮助某人.

I'm sure it'll help somebody one day.

这篇关于当 RichTextBox 刚加载/为空时,WPF EditingCommands 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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