Using MouseDragElementBehavior with an ItemsControl and Canvas

后端 未结 2 939
灰色年华
灰色年华 2021-01-22 00:37

I am currently having a problem using the MouseDragElementsBehavior from the Blend SDK when using a ItemsControl and a Custom Canvas. My custom canvas simply adds or removes th

相关标签:
2条回答
  • 2021-01-22 01:09

    I don't really know the blend sdk behaviours, but I've worked with behaviours in general, so I hope the same mechanisms apply.

    If you want to add a behaviour to the controls created by an ItemsControl the best way is adding it via a setter in the ItemsControl.ItemContainerStyle, though in this case I found it easier to add it in the ItemsControl.ItemTemplate

    Something like

            <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 讨论(0)
  • 2021-01-22 01:17
    <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 讨论(0)
提交回复
热议问题