Wpf defining custom properties for styles

I created a custom button using the Style and Control template. I would like to define some custom properties for this button, such as ButtonBorderColour and RotateButtonText.

How can I do it? Is it possible to do this only with XAML or does it require some kind of C # code?

+5
source share
1 answer

Properties must be declared in C # using DependencyProperty.Register (or, if you are not creating a custom tyoe button, DependencyProperty.RegisterAttached). Here's the declaration if you create a custom button class:

public static readonly DependencyProperty ButtonBorderColourProperty =
  DependencyProperty.Register("ButtonBorderColour",
  typeof(Color), typeof(MyButton));  // optionally metadata for defaults etc.

public Color ButtonBorderColor
{
  get { return (Color)GetValue(ButtonBorderColourProperty); }
  set { SetValue(ButtonBorderColourProperty, value); }
}

, , , RegisterAttached:

public static class ButtonCustomisation
{
  public static readonly DependencyProperty ButtonBorderColourProperty =
    DependencyProperty.RegisterAttached("ButtonBorderColour",
    typeof(Color), typeof(ButtonCustomisation));  // optionally metadata for defaults etc.
}

XAML:

<local:MyButton ButtonBorderColour="HotPink" />
<Button local:ButtonCustomisation.ButtonBorderColour="Lime" />
+4

All Articles