2012-03-15 21 views
0

我在畫布上添加了一個Line C#代碼以及一個上下文菜單和附加事件。我想利用上下文菜單的選擇,而不是在上下文菜單中的菜單文本旋轉Line菜單單擊事件對象參數引用菜單,而不是底層對象

newMenuItem1.PreviewMouseDown += new MouseButtonEventHandler((sx, ex) => { 
  MenuItem menuItem = (MenuItem)sx; 
      string theHeader = menuItem.Header.ToString(); 
      if (theHeader.Contains("90")) { 
       Line ow = ex.Source as Line; 
       rt = new RotateTransform(90, 25, 50); 
       ow.RenderTransform = rt; 
      } 

     }); 

此代碼生成一個null reference exception。如果我替換:

UIElement ow = ex.Source as UIElement;

實際的菜單文字會旋轉!

編輯: 這裏是更多的代碼,我現在想originalsource太:

private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     Line g = new Line(); 
     g.Stroke = System.Windows.Media.Brushes.LawnGreen; 
     g.X1 = 0; g.X2 = 100;g.Y1 = 0;g.Y2 = 0; 
     g.HorizontalAlignment = HorizontalAlignment.Left; 
     g.VerticalAlignment = VerticalAlignment.Center; 
     g.StrokeThickness = 6; 
     ContextMenu k = new ContextMenu(); 
     g.ContextMenu = k; 
     MenuItem newMenuItem1 = new MenuItem(); 
     MenuItem newMenuItem2 = new MenuItem(); 
     MenuItem newMenuItem3 = new MenuItem(); 
     newMenuItem1.Header = "Rotate 90"; 
     newMenuItem2.Header = "Rotate 180"; 
     newMenuItem3.Header = "Rotate 270"; 
     newMenuItem1.PreviewMouseDown += new MouseButtonEventHandler((sx, ex) => { 
      MenuItem menuItem = (MenuItem)sx; 
      string theHeader = menuItem.Header.ToString(); 
      if (theHeader.Contains("90")) { 
       Line ow = (Line)ex.OriginalSource; 
       rt = new RotateTransform(90, 25, 50); 
       ow.RenderTransform = rt; 
      } 

     }); 
     g.ContextMenu.Items.Add(newMenuItem1); 
     g.ContextMenu.Items.Add(newMenuItem2); 
     g.ContextMenu.Items.Add(newMenuItem3); 

     Canvas.SetTop(g, 18); 
     Canvas.SetLeft(g, 18); 

     MyCanvas.Children.Add(g); 

     /////// 

我也試過:

private static T FindAncestor<T>(DependencyObject current) 
    where T : DependencyObject 
    { 
     do 
     { 
      if (current is T) 
      { 
       return (T)current; 
      } 
      current = VisualTreeHelper.GetParent(current); 
     } 
     while (current != null); 
     return null; 
    } 

,但它不工作。我的下一個計劃是從畫布上獲取座標,並嘗試確定哪些控件位於那裏。雖然如果一個對象被轉換,這將變得棘手,因爲我相信UI會在原始位置看到它。我也嘗試過其他控件,例如TextBox,並獲得類似的問題。

+1

ex.Source是您在此案例中的菜單項。你想要旋轉一個Line類型的對象。你應該更好地描述這個課程,以及你如何將菜單添加到它。 – asktomsk 2012-03-15 15:53:05

+0

我加了一些更多的代碼,我也嘗試了原始資源。 – Len 2012-03-15 16:21:50

+0

請注意,上下文菜單不是與您的行相同的樹的一部分,所以FindAncestor不起作用並不奇怪。 – 2012-03-15 18:35:12

回答

1

一個非常快速和骯髒的方式做到這將是您的行添加到菜單項的標籤屬性並檢索它在PreviewMouseDown處理

在創建上下文菜單:

newMenuItem1.Tag = g; 

在你的處理程序:

Line ow = ((FrameworkElement)ex.Source).Tag as Line; 

越少快速和骯髒的方式做到這一點是使用您的線路的ContextMenuOpening事件作爲以t發送他的發件人等於控制本身。然後,您可以在某處存儲對該行的引用,並在菜單項單擊事件中再次檢索該引用。當你有多行(我猜是你想要的),只需要一個上下文菜單(而不是像你現在做的那樣產生一堆相同菜單的副本)時,這會更好。

+0

關閉時,我將它添加到menuItem時才起作用。 newMenuItem1.Tag = g; Line ow =((FrameworkElement)menuItem).Tag as Line; – Len 2012-03-15 19:40:16

+0

@恩:是的,你說得對。如果將它添加到上下文菜單中,則必須找到菜單項的父項,然後找到該標籤。我編輯了我的答案。但請注意,它仍然是「快速和骯髒」 – 2012-03-15 19:48:37