Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf defining custom properties for styles

Tags:

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

How do i go about this? Can it be done just using XAML or does it require some C# code behind work?

like image 781
Mathew Avatar asked Jan 30 '10 23:01

Mathew


1 Answers

The properties need to be declared in C# using DependencyProperty.Register (or, if you are not creating a custom button tyoe, DependencyProperty.RegisterAttached). Here's the declaration if you are creating 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); }
}

If you are not creating a custom class, but want to define properties that can be set on a normal Button, use RegisterAttached:

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

They can then be set in XAML:

<local:MyButton ButtonBorderColour="HotPink" />
<Button local:ButtonCustomisation.ButtonBorderColour="Lime" />
like image 145
itowlson Avatar answered Oct 12 '22 23:10

itowlson