Using MouseDragElementBehavior with ItemsControl and Canvas

I am currently having a problem using MouseDragElementsBehavior from the Blend SDK when using ItemsControl and Custom Canvas. My custom canvas simply adds or removes MouseDragElement from its children, depending on DependencyProperty. This worked very well when I manually added items to the children of the Canvas, but it seemed to break when moving to the ItemsControl.

I am currently using the following ItemsControl code:

<ItemsControl ItemsSource="{Binding Path=CanvasItems}"> <ItemsControl.DataContext> <ViewModels:ViewModel/> </ItemsControl.DataContext> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <my:CustomCanvas Background="Black" IsEditable="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CanEdit}" AllowDrop="{Binding RelativeSource={RelativeSource Self}, Path=IsEditable}" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> 

Adding drag and drop behavior to the Canvas.VisualChildrenChanged method does not allow you to move the newly created object, as before.

Do I need to add Drag behavior to something other than ContentPresenter, which is passed in by VisualChildrenChanged or provides a special style?

+2
source share
2 answers

I really don't know the behavior of bld sdk, but I worked with the behavior in general, so I hope the same mechanisms will apply.

If you want to add behavior to the controls created by the ItemsControl, the best way is to add it via setter to ItemControl.ItemContainerStyle, although in this case it was easier for me to add it to ItemControl.ItemTemplate

Sort of

  <ItemsControl ItemsSource="{Binding CanvasItems}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas Background="Transparent" AllowDrop="True" /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Border BorderBrush="Green" BorderThickness="1" Background="AntiqueWhite"> <i:Interaction.Behaviors> <ei:MouseDragElementBehavior ConstrainToParentBounds="True" DragBegun="MouseDragElementBehavior_DragBegun"/> </i:Interaction.Behaviors> <ContentControl Content="{Binding}" /> </Border> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> 
0
source
 <ItemsControl ItemsSource="{Binding CanvasItems}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style TargetType="YourControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="YourControl"> <Border> <Grid> <SystemWindowsInteractivity:Interaction.Behaviors> <MicrosoftExpressionInteractivityLayout:MouseDragElementBehavior /> </SystemWindowsInteractivity:Interaction.Behaviors> <ContentPresenter /> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ItemsControl.ItemContainerStyle> </ItemsControl> 
0
source

Source: https://habr.com/ru/post/1444075/


All Articles