保持 RichEditBox 中的选择视觉不聚焦? [英] Keep selection visual in RichEditBox on unfocus?

查看:22
本文介绍了保持 RichEditBox 中的选择视觉不聚焦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道在 RichEditBox 中保持所选文本的视觉选择状态的方法吗?我想向我的 Windows 8.1 应用程序添加一些基本的文本编辑,但每次我选择一个文本并单击应用程序中的另一个 UI 元素时,RichEditBox 都会隐藏选择.

Does someone know a way to keep the visual selection state of a selected text in a RichEditBox? I want to add some basic text editing to my Windows 8.1 App but every time I select a text and click on another UI Element within the app, the RichEditBox hides the selection.

我已经尝试注册 unfocus 事件并再次设置选择范围,但不幸的是这没有效果.

I already tried to register the unfocus event an set the selection range again but unfortunately this has no effect.

我还尝试使用

richEdit.Document.Selection.GetRect(PointOptions.ClientCoordinates,out selectionRect, out hitCount );

只要仅选择一行中的某些文本,此方法就有效.如果选择是多行的,我只会得到所选文本的左上角和右下角位置.似乎这些是选择开始和结束的鼠标位置.

This works as long as only some text within a single line is selected. If the selection is multilined I only get the top-left of and the bottom-right position of the selected Text. It seems like these are the mouse Positions where the selection where started and where it ended.

当 RichEditBox 未聚焦时,有没有其他方法可以保持所选文本可见.

Are there any other ways keep to the selected text visibible when the RichEditBox is unfocused.

推荐答案

我找到了另一种解决方法.只需在 RichEditBox 未聚焦时设置选择背景即可.但是 Jerry 的帖子给了我这个解决方案的灵感.似乎这种方式很容易找到第一个位置:

I found another workaround. Just set the selection Background when the RichEditBox is unfocused. But Jerry's Post gave me the Inspiration to this solution. Seems like this way was to simple to find it a first place:

private void RichEditOnGotFocus(object sender, RoutedEventArgs routedEventArgs)
{
    ITextSelection selectedText = richEdit.Document.Selection;
    if (selectedText != null)
    {
        richEdit.Document.Selection.SetRange(_selectionStart, _selectionEnd);
        selectedText.CharacterFormat.BackgroundColor = Colors.White;
    }
}

private void RichEditOnLostFocus(object sender, RoutedEventArgs routedEventArgs)
{
    _selectionEnd = richEdit.Document.Selection.EndPosition;
    _selectionStart = richEdit.Document.Selection.StartPosition;

    ITextSelection selectedText = richEdit.Document.Selection;
    if (selectedText != null)
    {
        selectedText.CharacterFormat.BackgroundColor = Colors.Gray;
    }
}

这篇关于保持 RichEditBox 中的选择视觉不聚焦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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