具有静态属性的子属性的绑定事件处理程序
问题描述:
在我的应用程序中有一个具有属性MySelectionChangedEvent的静态对象SomeClass.Current。在WPF中,我需要将ListBox SelectionChanged事件绑定到这个子属性。对于静态物体的不-A-处理器性能这个绑定工作正常,但不能用于处理程序:具有静态属性的子属性的绑定事件处理程序
<ListBox SelectionChanged="{Binding Path=MySelectionChangedEvent, Source={x:Static SomeClass.Current}}" ...></ListBox>
,我试图以这些方式来声明MySelectionChangedEvent:
public EventHandler<SelectionChangedEventArgs> MySelectionChangedEvent{get;set;}
或
public event EventHandler<SelectionChangedEventArgs> MySelectionChangedEvent;
或
public static readonly DependencyProperty MySelectionChangedEventProperty =
DependencyProperty.Register("MySelectionChangedEvent",
typeof(EventHandler<SelectionChangedEventArgs>),
typeof(SomeClass),
new PropertyMetadata(new EventHandler<SelectionChangedEventArgs>((s, e) => { })));
但是,一切都导致了运行时错误:
Cannot find DependencyProperty or PropertyInfo for property named 'MySelectionChangedEvent'. Property names are case sensitive. Error at object 'System.Windows.Controls.ListBox' in markup file 'DbEditor;component/...'
什么是事件处理程序绑定到静态对象的属性(或域),正确的方法?
答
简单地通过从标准生成方法调用我的自定义处理程序来解决问题。
你是否尝试过ListBox风格的EventSetter? – Bijan 2014-11-24 10:44:27
可能重复的http://*.com/questions/6430256/wpf-event-binding-to-viewmodel-for-non-command-classes – Bijan 2014-11-24 10:48:18
@Bizz,那里没有关于静态属性的词,但答案是那个问题对我来说也是正确的。 – FLCL 2014-11-24 10:52:50