My program requires a drag-and-drop. when i move a ‘part’ rectangle into another ‘raw material’ rectangle, it’s successfully added to where i dropped but i need to remove the moved part from its original location. but i am struggling to get the data of the raw material where the dragged part belonged to… i guess it’s because part rectangle is not directly parented with the raw material rectangle but i have no idea how to solve this ????
AXAML
<!-- Parts arranged inside raw materials -->
<StackPanel Orientation="Vertical" DragDrop.AllowDrop="True">
<ItemsControl Name="RawMaterialList" ItemsSource="{Binding DragAndDropData.ArrangedRawMaterials}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas Width="1200" Height="40">
<!-- Raw material -->
<Border BorderBrush="Black" BorderThickness="1">
<Rectangle Name="RawMaterialRectangle" Height="30" Width="{Binding Length, Converter={StaticResource DivideByTenConverter}}" Fill="LightGray" Canvas.Left="0" Canvas.Top="5"
DragDrop.AllowDrop="True" Tag="{Binding}" />
</Border>
<!-- Parts -->
<ItemsControl ItemsSource="{Binding PartsInside}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Black" BorderThickness="1">
<Rectangle Name="PartRectangle" Height="30" Width="{Binding Length, Converter={StaticResource DivideByTenConverter}}" Fill="BurlyWood"
PointerPressed="Part_PointerPressed" DragDrop.AllowDrop="True" Tag="{Binding }"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
- The data structure of Bound ItemsSource:
- A list of RawMaterial (‘ArrangedRawMaterials’ in the code)
- RawMaterial contains a list of Parts (‘PartsInside’ in the code)
Drag-And-Drop Event Handler
private void Part_PointerPressed(object sender, PointerPressedEventArgs e)
{
var part = (sender as Control)?.DataContext as Part;
var partRectangle = sender as Rectangle;
var rawMaterialRectangle = partRectangle?.GetVisualParent<Rectangle>();
var rawMaterial = rawMaterialRectangle?.Tag as RawMaterial;
- rawMaterial is always null…
- and if i remove the rectangle type from the GetVisualParent, it selects the border tag that wraps the part rectangle tag