我正在尝试在自定义控件内部移动椭圆,并且我关心它的中心点,因此我在EllipseGeometry
内部使用Path
而不是单独使用Ellipse
。
因此,我创建了这个自定义控件(CenterPoint的编码为100,100):
Public Class RippleButton
Inherits ButtonBase
Shared Sub New()
DefaultStyleKeyProperty.OverrideMetadata(GetType(RippleButton), New FrameworkPropertyMetadata(GetType(RippleButton)))
End Sub
Public Sub New()
MyBase.New()
CenterPoint = New Point(100, 100)
End Sub
Public Property CenterPoint As Point
Get
Return DirectCast(GetValue(CenterPointProperty), Point)
End Get
Set(ByVal value As Point)
SetValue(CenterPointProperty, value)
End Set
End Property
Public Shared ReadOnly CenterPointProperty As DependencyProperty =
DependencyProperty.Register("CenterPoint", _
GetType(Point), GetType(RippleButton))
End Class
此类的默认样式(EllipseGeometry的Center绑定到控件中的CenterPoint):
<Style TargetType="{x:Type local:RippleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:RippleButton}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Path Fill="Red" ClipToBounds="True">
<Path.Data>
<EllipseGeometry Center="{TemplateBinding CenterPoint}"
RadiusX="100"
RadiusY="100" />
</Path.Data>
</Path>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
将我的RippleButton放在WPF窗口上时,红色椭圆始终位于左上角,而不是100,100。这是怎么回事,如何将其移动到类中定义的CenterPoint?
最佳答案
不知道为什么它不能与TemplateBinding
一起使用,但是您可以将其替换为常规绑定:
<EllipseGeometry
Center="{Binding CenterPoint, RelativeSource={RelativeSource TemplatedParent}}"
RadiusX="100" RadiusY="100" />
关于wpf - 如何绑定(bind)到EllipseGeometry的Center属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24534148/