【问题标题】:Positioning listbox within a richtextbox在富文本框中定位列表框
【发布时间】:2011-12-29 04:25:53
【问题描述】:

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

我该怎么做?

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

【问题讨论】:

    标签: c# wpf listbox margin


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的,我已经尝试了一段时间:/ 感谢您的帖子!
    【解决方案2】:

    这是我要做的(用你的 ListBox 替换我的 Rectangle):

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

    【讨论】:

    • 非常感谢!我正在寻找这样的东西:)
    • 太棒了。我查看了 API,但一定错过了这一点。优秀作品。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    相关资源
    最近更新 更多