我正在研究状态机测试设计工具,但是我面临着在设计面板中拖放添加节点的问题。因为这些节点是在运行时添加的。我不能为每个添加的节点实现拖放事件,因为我知道我正在使用WPF与calibren。微平台和MVVM
XAML代码<!-- Column 2 : Design panel-->
<StackPanel x:Name="DesignPanelState" Grid.Row="1" Grid.Column="2" Grid.RowSpan="2" >
<ItemsControl x:Name="Nodes" MouseLeftButtonDown="Nodes_MouseLeftButtonDown">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1" Margin ="3" Padding="3">
<StackPanel Orientation="Vertical" MouseMove="Nodes_MouseMove">
<Button x:Name="NodeIdx" Content="{Binding NodeName}" MinWidth="50" MinHeight="50"/>
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
用户界面图片
我在https://www.codeproject.com/Articles/15354/Dragging-Elements-in-a-Canvas找到了我问题的答案
我将类DragCanvas.cs集成到我的项目中,并在XAML文件的头中包含以下行xmlns:雅="clr-namespace: WPF.JoshSmith.Controls"
和XAML代码变成
<!-- Column 2 : Design panel-->
<jas:DragCanvas x:Name="DesignPanelState"
Grid.Column="2" Grid.RowSpan="3"
AllowDragging="{Binding ElementName=Move, Path=IsChecked}"
AllowDragOutOfView="False">
<ItemsControl x:Name="Nodes">
<ItemsControl.ItemsPanel >
<ItemsPanelTemplate >
<Canvas AllowDrop="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate >
<DataTemplate>
<Button x:Name="NodeIdx" BorderBrush="Aquamarine" BorderThickness="1" Content="{Binding NodeName}" MinWidth="50" MinHeight="50"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</jas:DragCanvas>