将 MouseDragElementBehavior 与 ItemsControl 和 Canvas 结合使用
目前,在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我真的不知道混合 SDK 的行为,但我已经处理过一般的行为,所以我希望应用相同的机制。
如果您想向由 ItemsControl 创建的控件添加行为,最好的方法是通过 ItemsControl.ItemContainerStyle 中的 setter 添加它,尽管在这种情况下,我发现在 ItemsControl.ItemTemplate 中添加它更
容易
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