Custom UserControl Property used by child element

后端 未结 4 858
花落未央
花落未央 2020-12-12 04:06

I\'m trying to get some WPF concepts down, so I\'ve put together a simple example of what I\'m trying to do. I would like to set a custom property of a user control, and hav

4条回答
  •  渐次进展
    2020-12-12 04:55

    You set up a dependency property like this:

    Public Shared ReadOnly MouseOverBrushProperty As DependencyProperty = DependencyProperty.Register("MouseOverBrush", GetType(Brush), GetType(BrushableComboBox), New UIPropertyMetadata())
    Public Property MouseOverBrush() As Brush
        Get
            Return CType(GetValue(MouseOverBrushProperty), Brush)
        End Get
        Set(ByVal value As Brush)
            SetValue(MouseOverBrushProperty, value)
        End Set
    End Property
    

    And then in your xaml you do something like this

    Background="{TemplateBinding MouseOverBrush}"
    

    and you can set a default style outside of the control template like this: