【问题标题】:binding LinearGradientBrush StartPoint in a custom control在自定义控件中绑定 LinearGradientBrush StartPoint
【发布时间】:2011-10-24 04:23:54
【问题描述】:

在上一个问题中,kbmax 告诉我如何在我的 generic.xaml 中绑定自定义属性。这个答案在Setting Border background with a template binding

在我尝试使用 LinearGradientBrush StartPoint 执行此操作之前效果很好,这是一个示例:

这显然工作正常:

<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">

我想将它们绑定到这样的自定义属性: 这不起作用,无论我将属性设置为什么,我总是得到一个从左到右的渐变。

<LinearGradientBrush StartPoint="{Binding HeaderBorderGradientStartPoint, RelativeSource={RelativeSource TemplatedParent}}" EndPoint="{Binding HeaderBorderGradientEndPoint, RelativeSource={RelativeSource TemplatedParent}}">

这是类中的代码。

    public static readonly DependencyProperty HeaderBorderGradientStartPointProperty =
        DependencyProperty.Register("HeaderBorderGradientStartPoint", typeof(Point), typeof(GalleryExpander), new PropertyMetadata(new Point(0.5,0)));
    public Point HeaderBorderGradientStartPoint {
        get { return (Point)GetValue(HeaderBorderGradientStartPointProperty); }
        set { SetValue(HeaderBorderGradientStartPointProperty, value); }
    }

    public static readonly DependencyProperty HeaderBorderGradientEndPointProperty =
        DependencyProperty.Register("HeaderBorderGradientEndPoint", typeof(Point), typeof(GalleryExpander), new PropertyMetadata(new Point(0.5,1)));
    public Point HeaderBorderGradientEndPoint {
        get { return (Point)GetValue(HeaderBorderGradientEndPointProperty); }
        set { SetValue(HeaderBorderGradientEndPointProperty, value); }
    }


<Border.BorderBrush>
    <LinearGradientBrush StartPoint="{Binding HeaderBorderGradientStartPoint, RelativeSource={RelativeSource TemplatedParent}}" EndPoint="{Binding HeaderBorderGradientEndPoint, RelativeSource={RelativeSource TemplatedParent}}">
        <LinearGradientBrush.GradientStops>
            <GradientStop Offset="0.0" Color="Transparent" />
            <GradientStop Offset="0.25" Color="Transparent" />
            <GradientStop Offset="0.50" Color="Transparent" />
            <GradientStop Offset="0.75" Color="Transparent" />
            <GradientStop Offset="1.0" Color="Transparent" />
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>
</Border.BorderBrush>

感谢您的任何指导......

【问题讨论】:

    标签: c# silverlight binding lineargradientbrush


    【解决方案1】:

    嗯,我创建了一个小的自定义控件来检查它,它工作得很好。我的控件如下所示:

    public class MyGradientControl : Control
    {
        public static readonly DependencyProperty StartPointDProperty =
            DependencyProperty.Register("StartPointD", typeof (Point), 
            typeof (MyGradientControl), new PropertyMetadata(new Point(0.5, 0.5)));
    
        static MyGradientControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof (MyGradientControl), 
                new FrameworkPropertyMetadata(typeof (MyGradientControl)));
        }
    
        public Point StartPointD
        {
            get { return (Point) GetValue(StartPointDProperty); }
            set { SetValue(StartPointDProperty, value); }
        }
    }
    

    并且具有以下样式(在 Themes\generic.xaml 中):

    <Style TargetType="{x:Type local:MyGradientControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:MyGradientControl}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid VerticalAlignment="Stretch" 
                              HorizontalAlignment="Stretch">
                            <Grid.Background>
                                <LinearGradientBrush 
                                    StartPoint="{Binding StartPointD, 
                                                 RelativeSource={RelativeSource 
                                                      TemplatedParent}}" 
                                    EndPoint="0, 0">
                                    <GradientStop Color="Red" Offset="0"/>
                                    <GradientStop Color="White" Offset="1"/>
                                </LinearGradientBrush>
                            </Grid.Background>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    你们是否为 LinearGradientBrush 提供 GradientStops?他们有偏移量吗?

    【讨论】:

    • 谢谢,我会将我的实际 LinearGradientBrush 放在原始问题的底部,以便对其进行格式化。
    • 我真正看到的唯一区别是我从 ContentControl 派生了我的控件,而在构造函数中我只有 this.DefaultStyleKey = typeof(GalleryExpander);当您从控件派生并具有 DefaultStyleKeyProperty.OverrideMetadata(typeof (MyGradientControl), new FrameworkPropertyMetadata(typeof (MyGradientControl)));在你的构造函数中
    • DefaultStyleKey 的更复杂的分配是 VisualStudio 在创建新的 CustomControl 时所做的。它实际上几乎是同一件事,只是它是在自定义控件的静态构造函数中完成的,而不是在实例构造函数中完成的。我刚刚尝试了两种方式,但没有任何区别(两种方式都很好)。从 ContentControl 而不是 Control 派生也有效。你介意分享你的代码吗?我没有想法..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    相关资源
    最近更新 更多