在自定义控件中绑定 LinearGradientBrush StartPoint
在上一个问题中,kbmax 告诉我如何在 generic.xaml 中绑定自定义属性。这个答案位于使用模板绑定设置边框背景
它工作得很好,直到我尝试使用 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>
感谢您的任何指导...
In a previous question kbmax told me how I could bind custom properties in my generic.xaml. This answer was at Setting Border background with a template binding
It worked great until I tried to do this with a LinearGradientBrush StartPoint, Here is an example:
This obviously works fine:
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
I would like to bind them to a Custom Property like this:
This does not work, I always get a left to right gradient no matter what I set the properties to.
<LinearGradientBrush StartPoint="{Binding HeaderBorderGradientStartPoint, RelativeSource={RelativeSource TemplatedParent}}" EndPoint="{Binding HeaderBorderGradientEndPoint, RelativeSource={RelativeSource TemplatedParent}}">
Here is the code in the class.
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>
Thanks for any direction...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我创建了一个小的自定义控件来检查这一点,它工作得很好。我的控件如下所示:
并具有以下样式(在 Themes\generic.xaml 中):
您是否为 LinearGradientBrush 提供 GradientStops?他们有偏移吗?
Hm, I created a small custom control to check this and it works just fine. My control looks like this:
And has the following style (in Themes\generic.xaml):
Do you provide GradientStops for the LinearGradientBrush? Do they have offsets?