具有相对尺寸的矩形几何图形...如何实现?

10

我正在尝试为我创建的按钮控件模板复制现今如此时髦的"reflex"效果。

基本思路是创建一个带有从白色到透明的渐变填充的矩形,接着用一个矩形几何图形剪切这个半透明矩形的一部分。

问题是我不知道如何定义相对矩形几何。我通过定义一个大值(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>
1个回答

12

你可以使用 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>

我不知道那个XAML命名空间是用来干什么的,但你的应该是 http://schemas.microsoft.com/winfx/2006/xaml/presentation - Abe Heidebrecht
我有:xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"。我的解决方案中从未出现过“2007”。这是一个Windows Phone 8应用程序。 - Cœur
2
那就是问题所在。Silverlight、WP7、WP8或WinRT中都不存在MultiBindings。如果你问我,这是一个相当大的疏忽,但你可以通过类似这样的方法解决它:http://www.scottlogic.co.uk/blog/colin/2010/05/silverlight-multibinding-solution-for-silverlight-4/。 - Abe Heidebrecht

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接