我想要實現一組類似的附加行爲以用於WPF應用程序。 因爲他們都共享大量的樣板代碼,我不想重複每一個代碼,我想創建一個從它繼承的基本行爲。 但是,由於附加行爲中的所有內容都是靜態的,所以我不知道如何去做。附加行爲的繼承
舉例來說,採取這種行爲,執行上的mousedown的方法(真正的行爲當然會做一些不容易在一個事件處理完成):
public static class StupidBehavior
{
public static bool GetIsEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsEnabledProperty);
}
public static void SetIsEnabled(DependencyObject obj, bool value)
{
obj.SetValue(IsEnabledProperty, value);
}
// Using a DependencyProperty as the backing store for ChangeTooltip. This enables animation, styling, binding, etc...
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(StupidBehavior), new UIPropertyMetadata(false, IsEnabledChanged));
private static void IsEnabledChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
((UIElement)sender).MouseDown += { (o,e) => MyMethod(); };
}
private static void MyMethod()
{
MessageBox.Show("Boo");
}
}
現在,我想創建一種新的行爲,應該有一個不同的MyMethod實現,以及一些控制它的附加屬性。這應該怎麼做?
當孩子繼承'StupidBehavior'時,這總是一種遺憾。 – 2011-05-10 16:34:50