首页 > 解决方案 > avalon 编辑器中的工具提示位置

问题描述

我在 wpf avalon 编辑器中遇到问题,未设置工具提示的位置。

在 avalon 编辑器中,在键入单词空间后,我必须在单词附近设置工具提示。
但是我没有得到任何解决方案,因此在键入空格时工具提示会显示在单词附近。

toolTip.Placement = PlacementMode.Relative;
toolTip.Content = "aa";
toolTip.IsOpen = true;

标签: c#wpfavalonedit

解决方案


这是一个执行您要求的示例:

XAML

<Window x:Class="WpfApp1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:avalonedit="http://icsharpcode.net/sharpdevelop/avalonedit"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
     <avalonedit:TextEditor Name="TextEditor" KeyDown="TextEditor_KeyDown" />
</Window>

后面的代码:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WpfApp1
{
    using System.Windows.Controls.Primitives;

    public partial class Window1 : Window
    {
        private ToolTip toolTip;
        public Window1()
        {
            InitializeComponent();
            toolTip = new ToolTip { Placement = PlacementMode.Relative, PlacementTarget = TextEditor};
            TextEditor.ToolTip = toolTip;
        }

        private void TextEditor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Space)
            {
                var caret = this.TextEditor.TextArea.Caret.CalculateCaretRectangle();
                toolTip.HorizontalOffset = caret.Right;
                toolTip.VerticalOffset = caret.Bottom;
                toolTip.Content = "aa";
                toolTip.IsOpen = true;
            }
            else
            {
                toolTip.IsOpen = false;
            }
        }
    }
}

结果:

插入符号处的 WPF 工具提示定位


推荐阅读