2017-06-03 63 views
1

有一個自定義wpf按鈕,其中包含按下顏色和懸停顏色,但我不想一直設置它們,如何使按下顏色並將顏色默認顏色懸停在按鈕的背景上? `帶懸浮顏色和按顏色的wpf按鈕,如何將它們的默認值設置爲背景

public Brush HoverBackground 
     { 
      get { return (Brush)GetValue(HoverBackgroundProperty); } 
      set { SetValue(HoverBackgroundProperty, value); } 
     } 

     public static readonly DependencyProperty HoverBackgroundProperty = 
      DependencyProperty.Register("HoverBackground", typeof(Brush), typeof(CustomButton)); 

和一些定製propertys

用途: 我不想寫這一切

<xx:CustomButton Background="xxx",HoverBacground="xx"/> 

我只是想這個時間:<xx:CustomButton Background="xx"/>

那麼Hoverbackground將與Bacground相同

`

回答

0

您可以在DP的元數據中設置默認值。

public static readonly DependencyProperty HoverBackgroundProperty = 
     DependencyProperty.Register("HoverBackground", 
            typeof(Brush), 
            typeof(CustomButton), 
            new UIPropertyMetadata(Brushes.Blue)); 
+0

這是行不通的...我不能設置像這樣新的UIPropertyMetadata(背景) –

0

BackgroundPropertyPropertyMetadata而對其進行註冊,並在元數據設置PropertyChangedCallback。您可以設置HoverBckgroundProperty的值。

+0

我已經嘗試這種方法,但它會導致設置HoverBackground不工作。 MSDN文檔說,設置一個屬性的本地值將消除綁定 –

相關問題