Wpf DataTemplate中的MouseDragElementBehavior

Wpf DataTemplate中的MouseDragElementBehavior,wpf,datatemplate,Wpf,Datatemplate,所以我有一个问题。我正在尝试在列表框中使用MouseDragElementBehavior。当我直接在listbox中创建项目时,我能够使其正常工作,如本例所示: 但一旦我开始使用DataTemplate,它就停止工作了 你知道为什么吗?我真的搞不清DataTemplate会如何影响MouseDragElementBehavior。MouseDragElementBehavior作用于您附加到的FrameworkElement。在您的例子中,它是ContentPresenter包含的Border

所以我有一个问题。我正在尝试在列表框中使用MouseDragElementBehavior。当我直接在listbox中创建项目时,我能够使其正常工作,如本例所示:

但一旦我开始使用DataTemplate,它就停止工作了

你知道为什么吗?我真的搞不清DataTemplate会如何影响MouseDragElementBehavior。

MouseDragElementBehavior作用于您附加到的FrameworkElement。在您的例子中,它是ContentPresenter包含的Border元素,ContentPresenter是ItemsControl生成的容器。您已设置ConstrainToParentBounds=True,这将确保视觉效果不会显示在其容器(在本例中为ContentPresenter)之外。有几个选择,一些简单,一个可能不值得去做,但我确实想了一些东西

设置ConstrainToParentBounds=False。我假设您不希望边界离开ItemsControl,因此这可能不适合。 将ItemContainerStyle设置为将模板设置为并将交互添加到类似配置的ContentPresenter的样式。ItemsControl的基本实现使用普通ContentPresenter。这里需要注意的是,如果您不使用UI元素作为项目,则需要使用自定义项目控件将项目包装为一个项目。请参见: 测试项目 使用ItemsControl.ItemContainerStyle附加交互。这有点复杂,因为Interaction.Behaviors attached属性只有一个setter: 为此,我必须创建一个单独的附加属性AddCollectionsToSetter.Behaviors(读/写)和一个允许添加交互的BehaviorCollection

公共静态类AddCollectionsToSetter { 附加了区域行为依赖属性 ///获取要添加的行为。 公共静态行为集合GetBehaviorDependencyObject对象 { 返回BehaviorCollectionobj.GetValueBehaviorProperty; } ///设置要添加的行为。 公共静态无效SetBehaviorsDependencyObject对象,BehaviorCollection值 { obj.SetValueAddCollectionsToSetter.BehaviorsProperty,值; } ///的DependencyProperty备份存储。表示要添加的行为。 /// 公共静态只读DependencyProperty BehaviorsProperty= DependencyProperty.RegisterAttachedBehaviors、BehaviorCollection类型、AddCollectionsToSetter类型、new PropertyMetadatanull、BehaviorPropertyChanged; 私有静态无效行为PropertyChangedDependencyObject d,DependencyPropertyChangedEventArgs e { var oldBehaviors=BehaviorCollection.OldValue; var newBehaviors=behaviorcollection.NewValue; var interaction=interaction.GetBehaviorsd; interaction.RemoveAngeldBehaviors;//扩展方法,简单迭代并删除 interaction.AddRangenewBehaviors.Clone;//扩展方法,简单迭代和添加 } 附加了endregion行为依赖属性 } 公共类行为集合:FreezableCollection { 公共行为集合 :基本 { } 公共行为集合整数容量 :基本容量 { } 公共行为集合无数行为 :基本行为 { } }
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.Items>
            <Border Width="20" Height="20">

                    <i:Interaction.Behaviors>
                        <ei:MouseDragElementBehavior ConstrainToParentBounds="True"/>
                    </i:Interaction.Behaviors>

                    <Rectangle Fill="Red"/>

                </Border>
        </ItemsControl.Items>

    </ItemsControl>
    <ItemsControl Grid.Column="1" >

        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.Items>
            Test item
        </ItemsControl.Items>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Border Width="20" Height="20">

                    <i:Interaction.Behaviors>
                        <ei:MouseDragElementBehavior ConstrainToParentBounds="True"/>
                    </i:Interaction.Behaviors>

                    <Rectangle Fill="Red"/>

                </Border>
            </DataTemplate>
        </ItemsControl.ItemTemplate>

    </ItemsControl>