Pad*_*oti 10 wpf controls clipping controltemplate
我正试图在我正在创建的按钮的控制模板上复制如今时尚的"反射"效果.
基本思想是创建一个矩形,其渐变填充从白色到透明,然后用矩形几何体剪切一些半透明矩形.
问题是我不知道如何定义相对矩形几何.我通过定义一个大值(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>
Run Code Online (Sandbox Code Playgroud)
Abe*_*cht 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();
}
}
Run Code Online (Sandbox Code Playgroud)
并在你的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>
Run Code Online (Sandbox Code Playgroud)