绑定事件处理程序与静态属性的子属性

时间:2014-11-24 10:38:52

标签: c# wpf data-binding listbox

我的应用程序中有一个静态对象SomeClass.Current,它具有属性MySelectionChangedEvent。在WPF中,我需要将ListBox SelectionChanged事件绑定到此子属性。对于静态对象的非处理程序属性,此绑定可正常工作,但不适用于处理程序:

<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/...'

将事件处理程序绑定到静态对象的属性(或字段)的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

只需从标准生成的方法调用我的自定义处理程序即可解决问题。