如何将 属性 绑定到另一个 class 的 属性(没有 UI 控件)?

How to bind a property to the property of another class (with no UI Control)?

我花了几个小时 Google 研究我的简单任务。 我正在尝试将我的变量 TestString 绑定到 TestClass.MeinString.

如果我点击按钮“tb_tbBinding”,TestString 和 TestClass.MyString 应该保持相同的值。

相关代码:

public partial class Window_Test : Window, INotifyPropertyChanged
{
    public Window_Test()
    {
        InitializeComponent();
        DataContext = this;

        // Trying to bind TestClass.MeinString to TestString
        BindingOperations.SetBinding(TestClass, BindingTestClass.MeinStringProperty, new Binding("TestClass.MeinString") { UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });
    }



    string _TestString = "Hello World!";
    public string TestString
    {
        get
        {
            return _TestString;
        }
        set
        {
            _TestString = value;
            OnPropertyChanged("TestString");
        }
    }

    BindingTestClass _TestClass = new BindingTestClass("Hallo Lukas!");
    public BindingTestClass TestClass
    {
        get
        {
            return _TestClass;
        }
        set
        {
            _TestClass = value;
            OnPropertyChanged("TestClass");
        }
    }

    private void btn_testclasschanger_click(object sender, RoutedEventArgs e)
    {
        TestClass.MeinString = "Changed String!";
    }

    private void btn_teststringchanger_click(object sender, RoutedEventArgs e)
    {
        TestString = "Changed Class!";
    }

}

我的自定义Class:

 public class BindingTestClass : DependencyObject, INotifyPropertyChanged
    {
        public BindingTestClass(string myString)
        {
            MeinString = myString;
        }

        public string MeinString
        {
            get
            {
                return (string)GetValue(MeinStringProperty);
            }
            set
            {
                SetValue(MeinStringProperty, value);
                OnPropertyChanged("MeinString");
            }
        }

        public static readonly DependencyProperty MeinStringProperty = DependencyProperty.Register("MeinString", typeof(string), typeof(BindingTestClass), new FrameworkPropertyMetadata()
        {
            BindsTwoWayByDefault = true,
            DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
        });

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
}

谢谢大家!

尝试设置绑定的 Source 属性

BindingOperations.SetBinding(TestClass, BindingTestClass.MeinStringProperty, 
    new Binding("TestString") { Source=this, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged });

默认情况下绑定使用 .DataContext 作为绑定的来源,但是在您的情况下 TestClass 没有将其 .DataContext 设置为任何值。事实上,我什至不确定 DependencyObject.

上的 属性 是否有效

通常 .DataContext 继承自 WPF 可视化树中对象的父级,但由于 TestClass 不是您的可视化树的一部分,因此它没有任何可继承的对象。