【问题标题】:RectangleGeometry with relative dimensions... how?具有相对尺寸的 RectangleGeometry ......怎么样?
【发布时间】:2011-01-31 10:35:00
【问题描述】:

我正在尝试在我正在创建的按钮的控件模板上复制时下如此时尚的“反射”效果。

基本思想是创建一个矩形,其渐变填充从白色到透明,然后用矩形几何形状剪裁一些半透明矩形。

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

【问题讨论】:

    标签: wpf controls controltemplate clipping


    【解决方案1】:

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

    【讨论】:

    • 它告诉我:“命名空间“schemas.microsoft.com/client/2007".”中不存在名称“MultiBinding”。”
    • 我不知道那个 xaml 命名空间是干什么用的,但你的应该是 http://schemas.microsoft.com/winfx/2006/xaml/presentation
    • 我有:xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"。我的解决方案中没有一次出现“2007”。这是一个 Windows Phone 8 应用程序。
    • 这就是问题所在。 Silverlight、WP7、WP8 或 WinRT 中不存在 MultiBinding。如果你问我,这有点疏忽,但你可以通过以下方式解决它:scottlogic.co.uk/blog/colin/2010/05/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-06-03
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多