2

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?

4

2 に答える 2

0

ブレンドSDKの動作はよくわかりませんが、一般的な動作を扱ってきたので、同じメカニズムが適用されることを願っています。

ItemsControlによって作成されたコントロールに動作を追加する場合は、ItemsControl.ItemContainerStyleのセッターを介して動作を追加するのが最善の方法ですが、この場合は、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>
于 2011-06-15T15:07:59.423 に答える
-1
<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>
于 2016-03-11T16:41:27.900 に答える