在 RichTextBox 中定位列表框

发布于 2024-12-15 09:54:42 字数 119 浏览 3 评论 0原文

我有一个富文本框,里面有一个列表框。我希望列表框位于插入符号的正下方,并随着插入符号的移动而移动。

我该怎么做?

我应该操作 listBox.Margin 的前两个值以及如何操作? 谢谢你!

I have a richtextbox and inside I have a listbox. I would like the listbox to be positioned just underneath the caret and to move as the caret moves.

How can I do this?

Should I manipulate the first two values of listBox.Margin and how?
Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见终念 2024-12-22 09:54:42

这就是我要做的(用你的列表框替换我的矩形):

<Window
    x:Class="Wpf_Playground.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Height="350"
    Width="525">
    <Grid>
        <RichTextBox
            Margin="0,0,0,32"
            x:Name="rtb"
            SpellCheck.IsEnabled="True"
            SelectionChanged="RtbSelectionChanged"
            TextChanged="RtbTextChanged">
        </RichTextBox>
        <Rectangle
            x:Name="rect"
            Width="30"
            Height="30"
            Fill="#80000000"
            VerticalAlignment="Top"
            HorizontalAlignment="Left"
            IsHitTestVisible="False"/>
        <TextBlock
            x:Name="tb"
            Margin="0"
            VerticalAlignment="Bottom" />
    </Grid>
</Window>

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Wpf_Playground
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        private void RtbSelectionChanged(object sender, RoutedEventArgs e)
        {
            this.UpdateCaretInfo();
        }

        /// <summary>
        /// The update caret info.
        /// </summary>
        private void UpdateCaretInfo()
        {
            var caretRect =
                rtb.CaretPosition.GetCharacterRect(LogicalDirection.Forward);
            tb.Text = caretRect.ToString();

            rect.Margin = new Thickness(
                caretRect.Right, 
                caretRect.Bottom, 
                -caretRect.Right, 
                -caretRect.Bottom);
        }

        private void RtbTextChanged(object sender, TextChangedEventArgs e)
        {
            this.UpdateCaretInfo();
        }
    }
}

Here's what I would do (replace my Rectangle with your ListBox):

<Window
    x:Class="Wpf_Playground.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Height="350"
    Width="525">
    <Grid>
        <RichTextBox
            Margin="0,0,0,32"
            x:Name="rtb"
            SpellCheck.IsEnabled="True"
            SelectionChanged="RtbSelectionChanged"
            TextChanged="RtbTextChanged">
        </RichTextBox>
        <Rectangle
            x:Name="rect"
            Width="30"
            Height="30"
            Fill="#80000000"
            VerticalAlignment="Top"
            HorizontalAlignment="Left"
            IsHitTestVisible="False"/>
        <TextBlock
            x:Name="tb"
            Margin="0"
            VerticalAlignment="Bottom" />
    </Grid>
</Window>

using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Wpf_Playground
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
        }

        private void RtbSelectionChanged(object sender, RoutedEventArgs e)
        {
            this.UpdateCaretInfo();
        }

        /// <summary>
        /// The update caret info.
        /// </summary>
        private void UpdateCaretInfo()
        {
            var caretRect =
                rtb.CaretPosition.GetCharacterRect(LogicalDirection.Forward);
            tb.Text = caretRect.ToString();

            rect.Margin = new Thickness(
                caretRect.Right, 
                caretRect.Bottom, 
                -caretRect.Right, 
                -caretRect.Bottom);
        }

        private void RtbTextChanged(object sender, TextChangedEventArgs e)
        {
            this.UpdateCaretInfo();
        }
    }
}
面犯桃花 2024-12-22 09:54:42

我不确定如何获取插入符的位置(尽管这是一个很好的问题,我很想知道如何获取),但我确实知道 RichTextBox 不能包含子元素。

我假设解决方案是将 RichTextBox 和 ListBox 放入 Canvas 中,并在每次 RichTextBox 的文本更改时将 ListBox 定位在插入符的位置。

但同样,我不知道如何检索插入符号的位置。 :/

I'm not sure about how to get the position of the caret (although this is a GREAT question, and I'd love to find out how), but I do know that RichTextBox cannot contain child elements.

I assume that the solution would be along the lines of putting the RichTextBox and the ListBox in a Canvas, and positioning the ListBox at at the location of the Caret each time the RichTextBox's text changes.

But again, I don't know how to retrieve the position of the caret. :/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文