[英]Using ItemsControl to bind buttons top/left position on canvas Windows Phone
I am using ItemsControl and have several buttons and place them at a specific location on the canvas. 我正在使用ItemsControl并具有多个按钮,并将它们放置在画布上的特定位置。 However, the
Canvas.Left
and Canvas.Top
always place all buttons at 0,0
. 但是,
Canvas.Left
和Canvas.Top
始终将所有按钮置于0,0
。
<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>
The best answer I've found for this is to use a RenderTransform in your datatemplate 我为此找到的最佳答案是在您的数据模板中使用RenderTransform
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.