将 MouseDragElementBehavior 与 ItemsControl 和 Canvas 结合使用

发布于 2024-11-15 16:44:26 字数 990 浏览 3 评论 0原文

目前,在使用 ItemsControl 和自定义画布时,我在使用 Blend SDK 中的 MouseDragElementsBehavior 时遇到问题。我的自定义画布只是根据 DependencyProperty 从其子级添加或删除 MouseDragElement。当我手动将 Items 添加到 Canvas 的子项时,这工作得很好,但在移动到 ItemsControl 时似乎已损坏。

我当前正在使用以下 ItemsControl 代码:

<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>

在 Canvas.VisualChildrenChanged 方法中添加拖动行为不允许像以前一样移动新创建的对象。

我是否需要将“拖动”行为添加到传递给 VisualChildrenChanged 的​​ ContentPresenter 之外的其他内容或提供特殊样式?

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 the MouseDragElement from its children depending on a DependencyProperty. This worked just fine when I was manually adding Items to the Canvas' children but appears to have broken when moving to an 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 the Drag Behavior in the Canvas.VisualChildrenChanged method does not allow the newly created object to be moved like before.

Do I need to add the Drag behavior to something other then the ContentPresenter that is passed to VisualChildrenChanged or provide a special style?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

林空鹿饮溪 2024-11-22 16:44:26

我真的不知道混合 SDK 的行为,但我已经处理过一般的行为,所以我希望应用相同的机制。

如果您想向由 ItemsControl 创建的控件添加行为,最好的方法是通过 ItemsControl.ItemContainerStyle 中的 setter 添加它,尽管在这种情况下,我发现在 ItemsControl.ItemTemplate 中添加它更

容易

        <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>

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>
一世旳自豪 2024-11-22 16:44:26
<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>
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文