2013-10-11 117 views
0

所以,我的問題是我試圖在畫布中移動一些usercontrols。在WPF中Mousemoving Usercontrol WPF

這實際上工作得很好,只要鼠標指針位於dockpanel內,這是usercontrol的組成部分。但是,在dockpanel中,有幾個itemscontrols,如果我點擊這些,並嘗試移動它,會產生一個異常,說明類似於「無法強制轉換類型爲」System.String「的對象來鍵入」UMLDesigner.Model 。.Node」 這是有道理的,但有沒有辦法讓DockPanel中,而不是ItemsControl的,即使它是被點擊的ItemsControl的

有相關的C#代碼:

public void MouseMoveNode(MouseEventArgs e) 
    { 
     //Is the mouse captured? 
     if (Mouse.Captured != null) 
     { 

      FrameworkElement movingClass = (FrameworkElement)e.MouseDevice.Target; 

      Node movingNode = (Node)movingClass.DataContext; 

      Canvas canvas = FindParent<Canvas>(movingClass); 
      Point mousePosition = Mouse.GetPosition(canvas); 
      if (moveNodePoint == default(Point)) moveNodePoint = mousePosition; 
      movingNode.X = (int)mousePosition.X; 
      movingNode.Y = (int)mousePosition.Y; 
     } 
    } 

    public void MouseUpNode(MouseEventArgs e) 
    { 
     //Used to move node 
     FrameworkElement movingClass = (FrameworkElement)e.MouseDevice.Target; 
     Node movingNode = (Node)movingClass.DataContext; 
     Canvas canvas = FindParent<Canvas>(movingClass); 
     Point mousePosition = Mouse.GetPosition(canvas); 
      new MoveNodeCommand(movingNode, (int)mousePosition.X, (int)mousePosition.Y,  (int)moveNodePoint.X, (int)moveNodePoint.Y); 
     moveNodePoint = new Point(); 
     e.MouseDevice.Target.ReleaseMouseCapture(); 
    } 

以及用於某些用戶控件的xaml:

<DockPanel.Background> 
      <LinearGradientBrush StartPoint="0,0" EndPoint="1,0.0"> 
       <LinearGradientBrush.GradientStops> 
        <GradientStop Color="Azure" Offset="0"/> 
        <GradientStop Color="Transparent" Offset="1"/> 
       </LinearGradientBrush.GradientStops> 
      </LinearGradientBrush> 
     </DockPanel.Background> 
     <TextBox Text="{Binding ClassName}" HorizontalAlignment="Center"  DockPanel.Dock="Top" Margin="5,0,5,0"/> 

     <!--Note the " : " is acutally being written to the GUI--> 
     <ItemsControl Name="attributeList" ItemsSource="{Binding Attributes}" Margin="5,0,5,0" DockPanel.Dock="Top"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <TextBlock><Run Text="{Binding Path=.}"/> : <Run Text="Type her"/></TextBlock> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

     <ItemsControl Name="propertiesList" ItemsSource="{Binding Properties}" Margin="5,0,5,0" DockPanel.Dock="Top"> 
     </ItemsControl> 

     <ItemsControl Name="methodsList" ItemsSource="{Binding Methods}" Margin="5,0,5,0" DockPanel.Dock="Top"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <TextBlock><Run Text="{Binding Path=.}"/>() : <Run Text="Type her"/></TextBlock> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

    </DockPanel> 

我當然也想知道是否有更智能或更好的方法來做到這一點。

回答

1

IsHitTestVisible="False"添加到ItemsControl。

+0

謝謝你。但是,在將來我可能需要能夠雙擊它們......雖然還不確定。但是,謝謝! –