2011-09-09 26 views
1

我可以渲染項目的集合並使用鼠標移動它們。我無法讓畫布項目面板自動調整大小。嘗試使用代碼,看看矩形是如何渲染的,以及如何用鼠標拖動它們。請注意,它們被限制爲父界(400x400)。接下來找到註釋行<!--<Canvas>-->取消註釋並註釋掉它上面的行<Canvas Height="400" Width="400">。現在請注意矩形是如何正確渲染的,但是隻要您將其中一個拖動到左上角並且無法再移動了!請幫忙!無法在自定義itemscontrol上自動調整畫布項目面板:每個元素都使用MouseDragElementBehavior

您將需要這些命名空間

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 

完全XAML低於,在放棄這一頁中的虛擬機上或在控制

<Grid x:Name="LayoutRoot"> 

    <UserControl> 

     <UserControl.Resources> 
      <DataTemplate x:Key="ItemTemplateKey"> 
       <Canvas Height="400" Width="400"> 
       <!--<Canvas>--> 
        <Rectangle Height="50" Width="50" Fill="Red"> 
         <i:Interaction.Behaviors> 
          <ei:MouseDragElementBehavior ConstrainToParentBounds="True"/> 
         </i:Interaction.Behaviors> 
        </Rectangle> 
       </Canvas> 
      </DataTemplate> 
     </UserControl.Resources> 

     <Grid x:Name="LayoutRoot2"> 
      <ItemsControl 
       ItemsSource="{Binding anArrayOfThreeOrFourThingsInTheVM}" 
       ItemTemplate="{StaticResource ItemTemplateKey}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <Canvas/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 
     </Grid> 

    </UserControl> 

</Grid> 

在或代碼,後面提供了一個集的ItemsSource綁定到:

public int[] anArrayOfThreeOrFourThingsInTheVM { get { return new int[] { 1, 2, 3 }; } } 

編輯: 耶穌寶寶!作爲睡前的最後一次努力,我嘗試用網格替換畫布,它一切正常!

這裏是萬一別人新的工作XAML有同樣的問題:

<Grid x:Name="LayoutRoot"> 

    <UserControl> 

     <UserControl.Resources> 
      <DataTemplate x:Key="ItemTemplateKey"> 
       <Grid> 
        <Rectangle Height="50" Width="50" Fill="Red"> 
         <i:Interaction.Behaviors> 
          <ei:MouseDragElementBehavior ConstrainToParentBounds="True"/> 
         </i:Interaction.Behaviors> 
        </Rectangle> 
       </Grid> 
      </DataTemplate> 
     </UserControl.Resources> 

     <Grid x:Name="LayoutRoot2"> 
      <ItemsControl 
       ItemsSource="{Binding anArrayOfThreeOrFourThingsInTheVM}" 
       ItemTemplate="{StaticResource ItemTemplateKey}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <Grid/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 
     </Grid> 

    </UserControl> 

</Grid> 
+1

......有啥正確的SO協議關閉這個問題?我是否應該自己發佈'答案'並將其標記爲答案? – dFlat

+1

是的,你自己添加答案(當它允許你)並且標記答案。請記住,有人可能會在某一天出現同樣的問題,你會爲他們回答這個問題! –

+0

我是那個人。感謝您提供答案! –

回答

2

我編輯上面這個回答我的問題。把它放在這裏來回答/關閉問題。

這裏是萬一別人新的工作XAML有同樣的問題:

<UserControl> 

    <UserControl.Resources> 
     <DataTemplate x:Key="ItemTemplateKey"> 
      <Grid> 
       <Rectangle Height="50" Width="50" Fill="Red"> 
        <i:Interaction.Behaviors> 
         <ei:MouseDragElementBehavior ConstrainToParentBounds="True"/> 
        </i:Interaction.Behaviors> 
       </Rectangle> 
      </Grid> 
     </DataTemplate> 
    </UserControl.Resources> 

    <Grid x:Name="LayoutRoot2"> 
     <ItemsControl 
      ItemsSource="{Binding anArrayOfThreeOrFourThingsInTheVM}" 
      ItemTemplate="{StaticResource ItemTemplateKey}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <Grid/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
     </ItemsControl> 
    </Grid> 

</UserControl> 

相關問題