Avalon编辑器中的工具提示位置

时间:2019-02-19 10:18:30

标签: c# wpf avalonedit

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

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

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

1 个答案:

答案 0 :(得分:0)

以下是您要执行的操作的示例:

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 tooltip positioning at caret