【问题标题】:Binding event handler with static property's sub property将事件处理程序与静态属性的子属性绑定
【发布时间】:2015-01-22 01:05:54
【问题描述】:

我的应用中有一个静态对象 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/...'

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

【问题讨论】:

  • 你试过 ListBox 风格的 EventSetter 了吗?
  • @Bizz,那里没有关于静态属性的消息,但这个问题的答案对我来说也是正确的。

标签: c# wpf data-binding listbox


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    相关资源
    最近更新 更多