在RichEditBox中保持选择的视觉效果是不重点?

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

我已经尝试注册非聚焦事件再次设置选择范围但不幸的是这没有效果。

我还试图在Text上绘制我自己的rect

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

只要选择一行中的某些文本,这就可以工作。 如果选择是多行的,我只获得所选文本的左上角和右下角。 看起来这些是鼠标位置,其中选择从哪里开始,在哪里结束。

当RichEditBox未聚焦时,还有其他方法可以保持所选文本的可见性。

我发现了另一种解决方法。 只需在RichEditBox未聚焦时设置选择背景。 但是Jerry’s Post给了我这个解决方案的灵感。 看起来像这样简单地找到它是第一位的:

 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; } } 

在WinRT Toolkit中,有一个HighlightBehavior不能直接解决您的问题,但可能会为您提供可接受的解决方案。

http://winrtxamltoolkit.codeplex.com/SourceControl/latest#WinRTXamlToolkit/WinRTXamlToolkit.Shared/Controls/Behaviors/HighlightBehavior.cs

 public class HighlightBehavior : Behavior { #region SearchString ///  /// SearchString Dependency Property ///  public static readonly DependencyProperty SearchStringProperty = DependencyProperty.Register( "SearchString", typeof(string), typeof(HighlightBehavior), new PropertyMetadata(null, OnSearchStringChanged)); ///  /// Gets or sets the SearchString property. This dependency property /// indicates the search string to highlight in the associated TextBlock. ///  public string SearchString { get { return (string)GetValue(SearchStringProperty); } set { SetValue(SearchStringProperty, value); } } ///  /// Handles changes to the SearchString property. ///  ///  /// The  on which /// the property has changed value. ///  ///  /// Event data that is issued by any event that /// tracks changes to the effective value of this property. ///  private static void OnSearchStringChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { var target = (HighlightBehavior)d; string oldSearchString = (string)e.OldValue; string newSearchString = target.SearchString; target.OnSearchStringChanged(oldSearchString, newSearchString); } ///  /// Provides derived classes an opportunity to handle changes /// to the SearchString property. ///  /// The old SearchString value /// The new SearchString value private void OnSearchStringChanged( string oldSearchString, string newSearchString) { UpdateHighlight(); } #endregion #region IsCaseSensitive ///  /// IsCaseSensitive Dependency Property ///  public static readonly DependencyProperty IsCaseSensitiveProperty = DependencyProperty.Register( "IsCaseSensitive", typeof(bool), typeof(HighlightBehavior), new PropertyMetadata(false, OnIsCaseSensitiveChanged)); ///  /// Gets or sets the IsCaseSensitive property. This dependency property /// indicates whether the highlight behavior is case sensitive. ///  public bool IsCaseSensitive { get { return (bool)GetValue(IsCaseSensitiveProperty); } set { SetValue(IsCaseSensitiveProperty, value); } } ///  /// Handles changes to the IsCaseSensitive property. ///  ///  /// The  on which /// the property has changed value. ///  ///  /// Event data that is issued by any event that /// tracks changes to the effective value of this property. ///  private static void OnIsCaseSensitiveChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { var target = (HighlightBehavior)d; bool oldIsCaseSensitive = (bool)e.OldValue; bool newIsCaseSensitive = target.IsCaseSensitive; target.OnIsCaseSensitiveChanged(oldIsCaseSensitive, newIsCaseSensitive); } ///  /// Provides derived classes an opportunity to handle changes /// to the IsCaseSensitive property. ///  /// The old IsCaseSensitive value /// The new IsCaseSensitive value private void OnIsCaseSensitiveChanged( bool oldIsCaseSensitive, bool newIsCaseSensitive) { UpdateHighlight(); } #endregion #region HighlightTemplate ///  /// HighlightTemplate Dependency Property ///  public static readonly DependencyProperty HighlightTemplateProperty = DependencyProperty.Register( "HighlightTemplate", typeof(DataTemplate), typeof(HighlightBehavior), new PropertyMetadata(null, OnHighlightTemplateChanged)); ///  /// Gets or sets the HighlightTemplate property. This dependency property /// indicates the template to use to generate the highlight Run inlines. ///  public DataTemplate HighlightTemplate { get { return (DataTemplate)GetValue(HighlightTemplateProperty); } set { SetValue(HighlightTemplateProperty, value); } } ///  /// Handles changes to the HighlightTemplate property. ///  ///  /// The  on which /// the property has changed value. ///  ///  /// Event data that is issued by any event that /// tracks changes to the effective value of this property. ///  private static void OnHighlightTemplateChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { var target = (HighlightBehavior)d; DataTemplate oldHighlightTemplate = (DataTemplate)e.OldValue; DataTemplate newHighlightTemplate = target.HighlightTemplate; target.OnHighlightTemplateChanged(oldHighlightTemplate, newHighlightTemplate); } ///  /// Provides derived classes an opportunity to handle changes /// to the HighlightTemplate property. ///  /// The old HighlightTemplate value /// The new HighlightTemplate value private void OnHighlightTemplateChanged( DataTemplate oldHighlightTemplate, DataTemplate newHighlightTemplate) { UpdateHighlight(); } #endregion #region HighlightBrush ///  /// HighlightBrush Dependency Property ///  public static readonly DependencyProperty HighlightBrushProperty = DependencyProperty.Register( "HighlightBrush", typeof(Brush), typeof(HighlightBehavior), new PropertyMetadata(new SolidColorBrush(Colors.Red), OnHighlightBrushChanged)); ///  /// Gets or sets the HighlightBrush property. This dependency property /// indicates the brush to use to highlight the found instances of the search string. ///  ///  /// Note that the brush is ignored if HighlightTemplate is specified ///  public Brush HighlightBrush { get { return (Brush)GetValue(HighlightBrushProperty); } set { SetValue(HighlightBrushProperty, value); } } ///  /// Handles changes to the HighlightBrush property. ///  ///  /// The  on which /// the property has changed value. ///  ///  /// Event data that is issued by any event that /// tracks changes to the effective value of this property. ///  private static void OnHighlightBrushChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { var target = (HighlightBehavior)d; Brush oldHighlightBrush = (Brush)e.OldValue; Brush newHighlightBrush = target.HighlightBrush; target.OnHighlightBrushChanged(oldHighlightBrush, newHighlightBrush); } ///  /// Provides derived classes an opportunity to handle changes /// to the HighlightBrush property. ///  /// The old HighlightBrush value /// The new HighlightBrush value private void OnHighlightBrushChanged( Brush oldHighlightBrush, Brush newHighlightBrush) { UpdateHighlight(); } #endregion private PropertyChangeEventSource _textChangeEventSource; ///  /// Called after the behavior is attached to an AssociatedObject. ///  ///  /// Override this to hook up functionality to the AssociatedObject. ///  protected override void OnAttached() { UpdateHighlight(); _textChangeEventSource = new PropertyChangeEventSource(this.AssociatedObject, "Text", BindingMode.OneWay); _textChangeEventSource.ValueChanged += TextChanged; base.OnAttached(); } ///  /// Called when the behavior is being detached from its AssociatedObject, but /// before it has actually occurred. ///  ///  /// Override this to unhook functionality from the AssociatedObject. ///  protected override void OnDetaching() { ClearHighlight(); _textChangeEventSource.ValueChanged -= TextChanged; _textChangeEventSource = null; base.OnDetaching(); } private void TextChanged(object sender, string s) { UpdateHighlight(); } ///  /// Updates the highlight. ///  public void UpdateHighlight() { if (this.AssociatedObject == null || string.IsNullOrEmpty(this.AssociatedObject.Text) || string.IsNullOrEmpty(this.SearchString)) { ClearHighlight(); return; } var txt = this.AssociatedObject.Text; var searchTxt = this.SearchString; var processedCharacters = 0; this.AssociatedObject.Inlines.Clear(); int pos; while ((pos = txt.IndexOf( searchTxt, processedCharacters, this.IsCaseSensitive ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase)) >= 0) { if (pos > processedCharacters) { var run = new Run { Text = txt.Substring( processedCharacters, pos - processedCharacters) }; this.AssociatedObject.Inlines.Add(run); } Run highlight; var highlightText = txt.Substring(pos, searchTxt.Length); if (this.HighlightTemplate == null) { highlight = new Run { Text = highlightText, Foreground = this.HighlightBrush }; } else { highlight = (Run)this.HighlightTemplate.LoadContent(); highlight.Text = highlightText; } this.AssociatedObject.Inlines.Add(highlight); processedCharacters = pos + searchTxt.Length; } if (processedCharacters < txt.Length) { var run = new Run { Text = txt.Substring( processedCharacters, txt.Length - processedCharacters) }; this.AssociatedObject.Inlines.Add(run); } } ///  /// Clears the highlight. ///  public void ClearHighlight() { if (this.AssociatedObject == null) { return; } var text = this.AssociatedObject.Text; this.AssociatedObject.Inlines.Clear(); this.AssociatedObject.Inlines.Add(new Run{Text = text}); } } public abstract class Behavior : Behavior where T : DependencyObject { #region Behavior() - CTOR ///  /// Initializes a new instance of the  class. ///  protected Behavior() { _associatedType = typeof(T); } #endregion #region AssociatedObject ///  /// Gets the object to which this  is attached. ///  public new T AssociatedObject { get { return (T)_associatedObject; } internal set { _associatedObject = value; } } #endregion } 

祝你好运!