2011-06-15 125 views
2

我當前在使用ItemsControl和自定義畫布時從Blend SDK使用MouseDragElementsBehavior時出現問題。我的自定義畫布根據DependencyProperty簡單地向它的子項添加或刪除MouseDragElement。當我手動將Items添加到畫布的子項時,這很好,但在移動到ItemsControl時似乎已經損壞。將MouseDragElementBehavior與ItemsControl和Canvas結合使用

我目前使用下面的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或提供特殊樣式的內容之外?

回答

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

這會,如果我在做一個拖/放(已implmented)工作... DragMouseElement用於在其容器中移動的對象。 – JMcCarty 2011-06-15 15:25:46

+0

不幸的是,我沒有Blend,所以我無法測試代碼。但我仍然會假設,MouseDragElementBehaviour會修改它附加的控件的Canvas.X附加屬性,所以它仍然應該工作。項目容器樣式應用於由ItemsControl生成的所有容器。我編輯了代碼以反映我見過的混合行爲的樣本。 – 2011-06-15 16:36:28

+0

您提供的樣式導致參數計數不匹配錯誤。你碰巧記得你在哪裏看到這些樣品,以便我可以看看? – JMcCarty 2011-06-15 17:43:30

0
<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> 
相關問題