我想要实现一组类似的附加行为以用于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