2010-03-24 60 views
10

我試圖在我正在創建的按鈕的控制模板上覆制時下如此時尚的「反射」效果。相對尺寸的矩形幾何......如何?

基本的想法是創建一個帶有從白色到透明的漸變填充的矩形,然後用矩形幾何圖形裁剪一些半透明矩形。

問題是我不知道如何定義相對矩形幾何。我通過定義一個大值(1000)來解決寬度問題,但高度是一個問題。例如,它適用於高度爲200的按鈕,但對於較小的按鈕不起作用。

任何想法?

  <Rectangle RadiusX="5" RadiusY="5" StrokeThickness="1" Stroke="Transparent"> 
       <Rectangle.Fill> 
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,0.55"> 
         <GradientStop Color="#66ffffff" Offset="0.0" /> 
         <GradientStop Color="Transparent" Offset="1.0" /> 
        </LinearGradientBrush> 
       </Rectangle.Fill> 
       <Rectangle.Clip> 
        <RectangleGeometry Rect="0,0,1000,60" /> 
       </Rectangle.Clip> 
      </Rectangle> 

回答

11

你可以用MultiBinding和新IMultiValueConverter做到這一點:

public class RectangleConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
{ 
     // you can pass in the value to divide by if you want 
     return new Rect(0, 0, (double)values[0], (double)values[1]/3.33); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 
} 

而且像這樣在您的XAML中:

<lcl:RectangleConverter x:Key="rectConverter" /> 

... 

<RectangleGeometry> 
    <RectangleGeometry.Rect> 
     <MultiBinding Converter="{StaticResource rectConverter}"> 
      <Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType={x:Type Button}}" /> 
      <Binding Path="ActualHeight" RelativeSource="{RelativeSource AncestorType={x:Type Button}}" /> 
     </MultiBinding> 
    </RectangleGeometry.Rect> 
</RectangleGeometry> 
+0

它告訴我:「這個名字 「MultiBinding」不存在於名稱空間「http://schemas.microsoft.com/client/2007」。「 – 2013-03-31 12:31:19

+0

我不知道xaml命名空間的用途是什麼,但你的應該是'http:// schemas.microsoft.com/winfx/2006/xaml/presentation'。 – 2013-03-31 15:06:19

+0

我有:'xmlns =「http://schemas.microsoft.com/winfx/2006/xaml/presentation」' 和'xmlns:x =「http://schemas.microsoft.com/winfx/2006/xaml 「'。我的解決方案中沒有一次是「2007」。這是一個Windows Phone 8應用程序。 – 2013-03-31 20:07:33