2014-09-29 364 views
0

我的想法是創建作爲動態磁貼(它渲染成位圖圖像和瓷磚背景使用)和我在LongListSelector應用程序內定製瓷磚控制。通過對兩種情況使用相同的控制,它確保它看起來完全相同。ScaleTransform不能正常工作

這裏我定製瓷磚控制的短樣本:

public class Tile : ContentControl 
{ 
    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 

     Content = GetTileLayout(); 
    } 

    private Grid GetTileLayout() 
    { 
     double width = 336; 
     double height = 336; 

     StackPanel panel = new StackPanel(); 
     TextBlock contentTextBlock = new TextBlock(); 
     // ...    

     Grid layoutRoot = new Grid(); 
     layoutRoot.Background = new SolidColorBrush(Colors.Red); 
     layoutRoot.VerticalAlignment = System.Windows.VerticalAlignment.Stretch; 
     layoutRoot.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch; 
     layoutRoot.Width = width; 
     layoutRoot.Height = height; 
     layoutRoot.Children.Add(panel); 
     layoutRoot.Measure(new Size(width, height)); 
     layoutRoot.Arrange(new Rect(0, 0, width, height)); 
     layoutRoot.UpdateLayout(); 

     return layoutRoot; 
    } 
} 

如果我想在LongListSelector那麼需要從336x336到200x200像素縮減到使用它。我嘗試了一個簡單的ScaleTransform,但結果與我預期的不一樣。

<phone:LongListSelector x:Name="ItemList" ItemsSource="{Binding Items}" LayoutMode="Grid" GridCellSize="222,222"> 
    <phone:LongListSelector.ItemTemplate>  
     <StackPanel Margin="12,12,0,0" Background="Blue"> 
      <DataTemplate> 
       <common:Tile VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> 
        <common:Tile.RenderTransform> 
         <ScaleTransform ScaleX="0.5" ScaleY="0.5" /> 
        </common:Tile.RenderTransform> 
       </common:Tile> 
      </DataTemplate> 
     </StackPanel> 
    </phone:LongListSelector.ItemTemplate> 
</phone:LongListSelector> 

這是結果:

enter image description here

紅色矩形是我定製瓷磚控制ScaleTransform。轉型後,它應該再次成爲一個正方形,從336x336縮小到168x168(只是一個例子)。在上面的圖片中,高度是正確的,但寬度太小。

,因爲如果我改變定製瓷磚控件的大小到200x200像素,並使用相同ScaleTransform以係數0.5,將正確地縮小這很奇怪。

回答

1

你的GridCellSize正在殺死變換。讓它更大,嘗試(400,400)。也擺脫了那個StackPanel。


<phone:LongListSelector.ItemTemplate> 
    <DataTemplate> 
     <Border BorderBrush="HotPink" BorderThickness="1,1,1,1" Tap="Border_Tap" HorizontalAlignment="Left" VerticalAlignment="Top"> 
       <Custom:Tile RenderTransformOrigin="0,0" HorizontalAlignment="Left" VerticalAlignment="Top"> 
        <Custom:Tile.RenderTransform> 
         <CompositeTransform ScaleX="0.5" ScaleY="0.5"/> 
        </Custom:Tile.RenderTransform> 
       </Custom:Tile> 
     </Border> 
    </DataTemplate> 
</phone:LongListSelector.ItemTemplate> 

enter image description here

+0

是的,你說得對,但爲什麼它只會影響寬度,而不是高度? – 2014-09-29 19:45:13

+0

如果我將ScaleTranform應用到由GetTileLayout()方法返回的Grid對象,它也不起作用。 – 2014-09-29 19:56:35

+0

另外,擺脫你所有的''它會正確地轉換它。我會稍微更新解決方案。 – 2014-09-29 21:19:53