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开发者_JAVA技巧 about this? Can it be done just using XAML or does it require some C# code behind work?
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" />
精彩评论