2010-01-30 84 views
5

我使用样式和控件模板创建了自定义按钮。我想定义这个按钮的一些自定义属性,例如ButtonBorderColour和RotateButtonText。wpf定义样式的自定义属性

我该如何解决这个问题?只能使用XAML来完成,还是需要一些C#代码?

回答

4

这些属性需要在C#中使用DependencyProperty.Register(或者,如果您没有创建自定义按钮Tyoe,DependencyProperty.RegisterAttached)来声明。下面是如果要创建一个自定义按钮声明:

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" />