簡體   English   中英

使用ItemsControl將按鈕綁定到畫布Windows Phone的頂部/左側位置

[英]Using ItemsControl to bind buttons top/left position on canvas Windows Phone

我正在使用ItemsControl並具有多個按鈕,並將它們放置在畫布上的特定位置。 但是, Canvas.LeftCanvas.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>

我為此找到的最佳答案是在您的數據模板中使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM