2014-01-29 3 views

답변

0

나는 당신이 언제 나타나는지에 달려 있다고 생각합니다. MSDN의 한 예는 RichTextBox 컨트롤 내에서 선택된 텍스트의 위치를 ​​ContextMenu과 어떻게 위치시키는 지 보여줍니다.

TextPointer position = rtb.Selection.End; 

if (position == null) return; 

Rect positionRect = position.GetCharacterRect(LogicalDirection.Forward); 
contextMenu.HorizontalOffset = positionRect.X; 
contextMenu.VerticalOffset = positionRect.Y; 

이것은 선택의 상대 위치를 가져옵니다 How to: Position a Custom Context Menu in a RichTextBox 흥미로운 비트는 아래의 코드가 될 것입니다. 양식을 팝업하는 경우이를 창 위치로 변환해야합니다.

이 코드는 RichTextBox에서 선택한 텍스트 위에 팝업 창을로드하는 테스트에 사용 된 코드입니다. 또한 다중 모니터도 고려합니다.

TextPointer tp = txtEditor.Selection.End; 
if (tp == null) return; 
Rect charRect = tp.GetCharacterRect(LogicalDirection.Forward); 
Point winPoint = txtEditor.PointToScreen(charRect.TopRight); 
Popup p = new Popup(); 
p.Left = winPoint.X; 
p.Top = winPoint.Y; 
p.Show(); 

UPDATE

: 나는 몇 가지 추가적인 연구를하고 지금까지 Popup 같은 행동을 찾고있는 것입니다 MSDN Popup Placement Behavior 기사를 발견했다. 위에 제공된 코드를 RichTextBox의 선택 또는 캐럿 위치와 함께 사용하여 Popup의 궁극적 인 위치를 결정할 수 있습니다. 도움이되기를 바랍니다.

0
static void tb_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) 
    { 


     if (e.KeyStates == ((e.KeyStates^System.Windows.Input.KeyStates.Down)^System.Windows.Input.KeyStates.Down)) 
     { 
      if (e.Key == System.Windows.Input.Key.OemPeriod) 
      { 

       TextBox tb = (TextBox)sender; 

       Rect r = tb.GetRectFromCharacterIndex(tb.CaretIndex, true); 
       Point p = tb.TransformToAncestor(tb).Transform(new Point(r.X, r.Y + 10)); 
       p = tb.PointToScreen(p); 

       Rect rect = new Rect(p.X, p.Y, 0, 0); 
       Grid g = (Grid)Application.Current.MainWindow.Content; 
       System.Windows.Controls.Primitives.Popup popup = new System.Windows.Controls.Primitives.Popup(); 
       popup.SetValue(System.Windows.Controls.Primitives.Popup.PlacementRectangleProperty, rect); 

       popup.IsOpen = true; 
       g.Children.Add(popup);}}} 
+3

답변에 대한 설명을 제공해 줄 수 있습니까? – soundslikeodd