wpf - 如何绑定(bind)到 EllipseGeometry 的 Center 属性?

标签 wpf xaml wpf-controls

我正在尝试在自定义控件内部移动椭圆,并且我关心它的中心点,因此我在 Path 内部使用 EllipseGeometry 而不是使用椭圆本身。

所以我创建了这个自定义控件(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 绑定(bind)到控件中的 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 一起使用,但您可以将其替换为常规绑定(bind):

<EllipseGeometry
    Center="{Binding CenterPoint, RelativeSource={RelativeSource TemplatedParent}}"
    RadiusX="100" RadiusY="100" />

关于wpf - 如何绑定(bind)到 EllipseGeometry 的 Center 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24534148/

相关文章:

c# - 理解调度队列

wpf - 如何以编程方式在 WPF TabControl 中选择 TabItem

wpf - TextBox.IsEnabled 绑定(bind)仅有效一次。为什么?

c# - 在 XAML 中禁用动画?

wpf - 如何将插入符号分配给 WPF 中的控件

WPF:列表框单击并拖动选择其他项目

wpf - 有没有更好的方法在 WPF 中继承窗口

xaml - 使用特定示例数据预览 xaml 布局

c# - 更改 TextBox 文本时 ViewModel 属性未更新

c# - 从 ViewMode/Viewl MVVM 发出保存模型