2011-11-15 7 views
4

リッチテキストボックスがあり、内側にリストボックスがあります。リストボックスをキャレットのすぐ下に配置し、キャレットが移動すると移動するようにしたいと思います。リッチテキストボックス内のリストボックスの位置付け

どうすればいいですか?

listBox.Marginの最初の2つの値を操作する必要がありますか? ありがとうございました!

答えて

4

ここで(あなたの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(); 
     } 
    } 
} 
+0

ありがとう!私はこのようなものを探していた: – gumenimeda

+0

サービスの喜んで –

+0

それは素晴らしいです。私はAPIを見渡しましたが、これを見逃しているに違いありません。優れた仕事。 :) –

0

キャレットの位置を取得する方法についてはわかりませんが(これは大きな質問ですが、どのように見つけたいか)、RichTextBoxには子要素を含めることはできません。

解決方法は、RichTextBoxとListBoxをキャンバスに配置し、RichTextBoxのテキストが変更されるたびに、そのキャレットの位置にListBoxを配置するという行に沿っていると想定します。

もう一度、キャレットの位置を取得する方法はわかりません。 :/

+0

うん、私はしばらくの間しようとしてきた:ポスト用/ありがとう! – gumenimeda