2014-11-24 58 views
0

在我的應用程序中有一個具有屬性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/...'

什麼是事件處理程序綁定到靜態對象的屬性(或域),正確的方法?

+0

你是否嘗試過ListBox風格的EventSetter? – Bijan 2014-11-24 10:44:27

+0

可能重複的http://stackoverflow.com/questions/6430256/wpf-event-binding-to-viewmodel-for-non-command-classes – Bijan 2014-11-24 10:48:18

+0

@Bizz,那裏沒有關於靜態屬性的詞,但答案是那個問題對我來說也是正確的。 – FLCL 2014-11-24 10:52:50

回答

0

簡單地通過從標準生成方法調用我的自定義處理程序來解決問題。

相關問題