2014-09-29 377 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