我知道您可以使用EventManager该类将类处理程序注册到路由事件,以便在为该类对象触发该事件时使类的所有实例做出响应:
EventManager.RegisterClassHandler( typeof( TextBox ), Control.MouseDoubleClickEvent,
new RoutedEventHandler( ( S, E ) => ( E.OriginalSource as TextBox )?.SelectAll( ) ) );
我正在寻找的是一种方法来做类似的事情,附加DependencyProperty到DataBinding使用该特定属性的控件类型的所有实例。
符合最小、完整和可验证示例要求...
应用程序.xaml.cs:
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace MCVE {
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : INotifyPropertyChanged {
public static readonly DependencyProperty FooProperty;
public event PropertyChangedEventHandler PropertyChanged;
static App( ) {
EventManager.RegisterClassHandler(
typeof( TextBox ), Control.MouseDoubleClickEvent,
new RoutedEventHandler(
( S, E ) => ( E.OriginalSource as TextBox )?.SelectAll( ) ) );
FooProperty = DependencyProperty.RegisterAttached(
"Foo", typeof( string ), typeof( App) );
//Is it possible to bind the FooProperty of all Window objects to
//the FooSource property defined below in similar fashion
//to how one can call RegisterClassHandler above?
}
[STAThread]
public static int Main( ) {
App program = new App( );
program.InitializeComponent( );
return program.Run( );
}
protected override void OnStartup( StartupEventArgs e ) {
this.FooSource = "Baz";
base.OnStartup( e );
}
public static string GetFoo( Window w ) =>
w.GetValue( FooProperty ).ToString( );
public static void SetFoo( Window w, string value ) =>
w.SetValue( FooProperty, value );
因此,所需的行为是,在启动程序后,之后创建的任何窗口都将通过某些数据绑定行为将FooProperty值设置为"Baz"(这样,如果FooSource属性更改,则每个窗口FooProperty属性也将更改)。
这可能吗?
胡子哥哥
相关分类