繁体   English   中英

失去焦点时,富文本框保持选中状态

[英]Rich text box keep selected highlighted when focus is lost

我创建通过使用简单的所见即所得的编辑器dockpanel的一些按钮,用于改变字体大小和这样的,和richtextbox为主要区域。 我可以突出显示文本,然后使用按钮更改字体大小和字体系列。 但是,当我单击“工具栏”时, RichTextBox失去焦点,并且所选文本不再突出显示。 我发现了一些e.handled =true;解决方案,例如设置e.handled =true; 在丢失焦点事件上,此方法有效,但例如,如果更改字体大小,则文本现在比突出显示的要大。 有更好的解决方案吗?

这是一些简化的代码:

    <DockPanel>
        <DockPanel >
            <ToggleButton Height="24" Margin="3" Name="Bold">
                <TextBlock FontWeight="ExtraBold" Text="B" />
            </ToggleButton>
            <ToggleButton Height="24" Margin="3" Name="Italic">
                <TextBlock FontStyle="Italic" Text="I" />
            </ToggleButton>
            <ToggleButton Height="24" Margin="3" Name="Underline">
                <TextBlock TextDecorations="Underline" Text="U" />
            </ToggleButton>
            <ComboBox Height="24" Margin="3" Name="FontFamily" Width="150" SelectionChanged="FontFamily_SelectionChanged"/>
            <ComboBox Height="24" Margin="3" Name="FontSize" Width="50" IsEditable="True" TextBoxBase.TextChanged="FontSize_TextChanged" />
        </DockPanel>
    </DockPanel>
    <RichTextBox Name="Editor" />

嗯,解决方案是简单地使用<toolbar>而不是<dockpanel>来容纳我的按钮。 这似乎正是我想要的。

尝试这个:

  private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        currentSize ++;
        RichTextBox.Selection.ApplyPropertyValue(TextElement.FontSizeProperty, currentSize);
        Keyboard.Focus(RichTextBox);
        RichTextBox.Selection.Select(RichTextBox.Selection.Start, RichTextBox.Selection.End);
    }

暂无
暂无

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

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