2013-06-20 25 views
0

我正在使用ItemsControl並有幾個按鈕,並將它們放在畫布上的特定位置。但是,Canvas.LeftCanvas.Top始終將所有按鈕都放在0,0使用ItemsControl綁定畫布上的按鈕頂部/左側位置Windows Phone

 <ItemsControl x:Name="itemsControl" ItemsSource="{Binding Tiles}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <Canvas /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Button Width="{Binding Side}" 
           Height="{Binding Side}" 
           Background="{Binding BgColor}"    
           Canvas.Left="{Binding Left}" Canvas.Top="{Binding Top}" />   
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 


     </ItemsControl> 
+0

不會它更好地使用'Margin'? –

+0

一般來說,這應該起作用。檢查你的綁定是否正常(即在調試過程中,你在輸出窗口中沒有任何綁定錯誤)。另一種方法是爲'ItemsPanelTemplate'嘗試使用'Grid'而不是'Canvas',將'Grid.Column'和'Grid.Row'綁定爲所需的值。 – Haspemulator

+0

我需要使用Canvas,因爲我需要將這些按鈕或圖形元素隨機放置在不包含列/行的頁面上 – PutraKg

回答

1

我發現這樣做的最好的答案就是使用的RenderTransform在你的DataTemplate

<ItemsControl x:Name="itemsControl" ItemsSource="{Binding Tiles}"> 
<ItemsControl.ItemsPanel> 
<ItemsPanelTemplate> 
<Canvas /> 
</ItemsPanelTemplate> 
</ItemsControl.ItemsPanel> 
<ItemsControl.ItemTemplate> 
<DataTemplate> 
<Button Width="{Binding Side}" Height="{Binding Side}" Background="{Binding BgColor}"> 
<Button.RenderTransform> 
<TranslateTransform X={Binding Left} Y ={Binding Top}/> 
</Button.RenderTransform>    
</Button>  
</DataTemplate> 
</ItemsControl.ItemTemplate> 
</ItemsControl> 
相關問題