C# 对于DependencyProperty值是否有类似于RegisterClassHandler的方法?

C# 对于DependencyProperty值是否有类似于RegisterClassHandler的方法?,c#,wpf,xaml,data-binding,dependency-properties,C#,Wpf,Xaml,Data Binding,Dependency Properties,我知道,您可以使用EventManager类向路由事件注册一个类处理程序,以便在为该类对象触发该事件时让该类的所有实例响应: EventManager.RegisterClassHandler( typeof( TextBox ), Control.MouseDoubleClickEvent, new RoutedEventHandler( ( S, E ) => ( E.OriginalSource as TextBox )?.SelectAll( ) ) ); 我正在寻找的是

我知道,您可以使用
EventManager
类向路由事件注册一个类处理程序,以便在为该类对象触发该事件时让该类的所有实例响应:

EventManager.RegisterClassHandler( typeof( TextBox ), Control.MouseDoubleClickEvent,
    new RoutedEventHandler( ( S, E ) => ( E.OriginalSource as TextBox )?.SelectAll( ) ) );
我正在寻找的是一种类似于附加的
dependencProperty
的方法,以向使用该特定属性的控件类型的所有实例提供
数据绑定

按照要求

App.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 );

        private string _fooSource = "Bar";
        public string FooSource {
            get => this._fooSource;
            set {
                this._fooSource = value;
                this.PropertyChanged?.Invoke(
                    this, new PropertyChangedEventArgs( "FooSource" ) );
            }
        }
    }
}
使用系统;
使用系统组件模型;
使用System.Windows;
使用System.Windows.Controls;
名称空间MCVE{
/// 
///App.xaml的交互逻辑
/// 
公共部分类应用程序:INotifyPropertyChanged{
公共静态只读从属属性FooProperty;
公共事件属性更改事件处理程序属性更改;
静态应用程序(){
EventManager.RegisterClassHandler(
typeof(TextBox),Control.MouseDoubleClickEvent,
新路线(
(S,E)=>(E.OriginalSource作为文本框)?。选择All());
FooProperty=DependencyProperty.RegisterAttached(
“Foo”、typeof(string)、typeof(App));
//是否可以将所有窗口对象的FooProperty绑定到
//下面以类似方式定义的FooSource属性
//如何调用上面的RegisterClassHandler?
}
[状态线程]
公共静态int Main(){
应用程序=新应用程序();
program.InitializeComponent();
返回program.Run();
}
启动时受保护的覆盖无效(StartupEventArgs e){
this.FooSource=“Baz”;
基础。启动时(e);
}
公共静态字符串GetFoo(窗口w)=>
w、 GetValue(FooProperty).ToString();
公共静态void SetFoo(窗口w,字符串值)=>
w、 SetValue(属性、值);
私有字符串\u fooSource=“Bar”;
公共字符串源{
get=>这个;
设置{
这个。_fooSource=值;
此.PropertyChanged?.Invoke(
这是新的PropertyChangedEventArgs(“FooSource”);
}
}
}
}
因此,期望的行为是,在启动程序后,随后创建的任何窗口都会通过一些数据绑定行为将
FooProperty
值设置为
“Baz”
(因此,如果
FooSource
属性更改,则每个窗口
FooProperty
属性也会更改)


这可能吗?

您只需要为
窗口添加一个事件处理程序。在
应用程序的构造函数中加载
事件

private App()
{
    EventManager.ResisterClassHandler(typeof(Window), Window.LoadedEvent,
        (RoutedEventHandler)((s, e) =>
            ((Windows)s).SetBinding(FooProperty, new Binding("FooSource")
            {
                Source = this,
            }
        ));
}

我相信这只是一个疏忽大意的失误,
FooProperty
的所有者类型应该是
typeof(App)
,而不是
typeof(Window)

我不知道我是否能很快回答。但这看起来很像oberser模式可以解决您的问题?还是我误解了这个问题?@MortenBork我甚至不太确定我知道你所说的“观察者模式”是什么意思。。。无论如何-问题中概述了问题-在本例中,创建的“窗口”的每个实例最终都应该将其附加的
Foo
属性绑定到指定的
FooSource
属性,最好不必为XAML中的每个窗口指定。我的意思是,我可以做到这一点,这就是我目前正在做的事情,但是由于
EventManager
方法,我觉得没有什么方式可以说“每个窗口都有这个值绑定到这个属性”。我不知道有什么具体的方法,但可以通过绑定的相对源属性来做到。或者你可以把foo属性放在一个单例对象或者甚至是一个resxx中,有一个带有自定义DP的自定义窗口怎么样?另外,如果您正在执行MVVM,每个窗口都可以有一个单独的datacontext,因此绑定将被中断。该物业的真正用途是什么?