2010-06-04 46 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