2010-06-04 29 views
2

我有一个包含椭圆的用户控件。将椭圆转换为右侧直到它部分位于其父控制之外。当父列宽设置为GridUnitType.Star时Silverlight剪裁UserControl

我把用户控件放到3列网格的中间列。

如果我将列宽设置为GridUnitType.Auto,我可以看到溢出列的椭圆。如果我将列宽设置为GridUnitType.Star,那么椭圆仍然会溢出列,但现在它被裁剪为列宽。

我需要使用GridUnitType.Star平均分配列宽,但不希望任何转换的内容被剪切。

我已经包含下面的示例代码。任何帮助,将不胜感激。

用户控件(含椭圆)

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
x:Class="GridWidthTest.UserControl1"> 

<Grid x:Name="LayoutRoot" Background="Green"> 
    <Ellipse Fill="#FFF40404" Stroke="Black" Grid.Column="1" Width="400" Height="400" RenderTransformOrigin="0.5,0.5"> 
     <Ellipse.RenderTransform> 
      <TransformGroup> 
       <TranslateTransform X="200"/> 
      </TransformGroup> 
     </Ellipse.RenderTransform> 
    </Ellipse>   
</Grid> 

父控制(含有网格)

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:GridWidthTest" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
x:Class="GridWidthTest.MainPage" 
Width="640" Height="480" mc:Ignorable="d"> 

<Grid x:Name="LayoutRoot" Background="White"> 

    <Grid ShowGridLines="True"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions>   

     <local:UserControl1 Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>    
    </Grid>   
</Grid> 

回答

0

这是解决farily棘手的问题。首先重要的是要了解RenderTransform实际上做了什么。它变换了已被渲染。基于最终相当简单的矩阵计算,将每个像素移动到新的位置。这在整个渲染过程中很晚才发生。该椭圆在之前已被剪切,应用该转换。

布局引擎经过两个阶段,措施各种元素争夺他们的首选条件(椭圆想要一个400×400盒来呈现),那么安排其中的元素通过它们的容器叫什么,他们可以实际上有。

在列的宽度是“自动” Grid屈服于期间测量阶段UserControl请求的宽度400的椭圆形使得没有任何限幅的盒的情况下,因为盒是足够大然后RenderTransform将结果转换为新的位置。

在列宽为“*”的情况下,Grid只会给UserControl一个由该列可用宽度份额确定的宽度框,即使该宽度小于UserControl所需的宽度。在这种情况下,椭圆渲染,但必须剪切到指定框的边框。它的然后这个剪切椭圆被翻译成一个新的位置。

为了能够跳出给定的框,元素可以指定负边距。这将导致一个更大的方框进行渲染。例如,如果容器指示宽度仅为200,但UserControl的边距为-100,则渲染的实际方框宽度为400像素。

让这个按预期工作,同时还支持动态安排内容可能是一个试验和错误的练习。

1

感谢您的回复。

我现在已经解决了这个问题,将椭圆包装在画布中,这意味着它不会被剪裁。

+0

巧妙!这很好地工作。 – McGarnagle 2014-10-22 22:45:11