Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在richtextbox中定位listbox_C#_Wpf_Listbox_Margin - Fatal编程技术网

C# 在richtextbox中定位listbox

C# 在richtextbox中定位listbox,c#,wpf,listbox,margin,C#,Wpf,Listbox,Margin,我有一个richtextbox,里面有一个列表框。我希望列表框被定位在插入符号的正下方,并随着插入符号的移动而移动 我该怎么做 我是否应该操作listBox.Margin的前两个值,以及如何操作? 谢谢大家! 我不确定如何获得插入符号的位置(虽然这是一个很好的问题,我很想知道如何获得),但我知道RichTextBox不能包含子元素 我假设解决方案是将RichTextBox和ListBox放在画布中,并在RichTextBox的文本每次更改时将ListBox定位在插入符号的位置 但是,我也不知道如

我有一个richtextbox,里面有一个列表框。我希望列表框被定位在插入符号的正下方,并随着插入符号的移动而移动

我该怎么做

我是否应该操作
listBox.Margin的前两个值,以及如何操作?

谢谢大家!

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

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

但是,我也不知道如何检索插入符号的位置:/

下面是我要做的(用列表框替换我的矩形):


使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
名称空间Wpf_游乐场
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口
{
/// 
///初始化类的新实例。
/// 
公共主窗口()
{
初始化组件();
}
private void RTB选择已更改(对象发送方,RoutedEventArgs e)
{
this.updatecartinfo();
}
/// 
///更新插入符号信息。
/// 
私有void updatecartinfo()
{
var caretRect=
rtb.CaretPosition.GetCharacterRect(LogicalDirection.Forward);
tb.Text=caretRect.ToString();
矩形边缘=新厚度(
小心,对,
小心点,底部,
-小心,对,
-caretRect.底部);
}
私有void RtbTextChanged(对象发送方,textchangedventargs e)
{
this.updatecartinfo();
}
}
}

非常感谢!我在找这样的东西:)太棒了。我查看了API,但肯定没有看到这个。出色的工作。:)是的,我已经试了一段时间了:/谢谢你的帖子!
<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();
        }
    }
}