定義WPF性能過長:尋找DependencyProperty.Register快捷
public static readonly DependencyProperty FooProperty =
DependencyProperty.Register("Foo", typeof(string), typeof(FooClass), new PropertyMetadata("Foooooo"));
我有一個輔助方法,使其成爲一個有點短:
public static readonly DependencyProperty FooProperty =
WpfUtils.Property<string, FooControl>("Foo", "Foooooo");
代碼:
public partial class WpfUtils
{
public static DependencyProperty Property<T, TClass>(string name)
{
return Property<T, TClass>(name, default(T));
}
public static DependencyProperty Property<T, TClass>(string name, T defaultValue)
{
return DependencyProperty.Register(name, typeof(T), typeof(TClass), new PropertyMetadata(defaultValue));
}
}
有更好的幫手嗎?
一件事在你的榜樣錯過的是富屬性包裝聲明,找出類型所需。但這太棒了! :-) – alex2k8 2009-03-01 13:26:48