简体   繁体   中英

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

I'm spending hours on Google researching my simple Task. I'm trying to do bind my variable TestString to TestClass.MeinString .

If I click on the button " tb_tbBinding " TestString and TestClass.MyString should stay the same value.

Relevant code:

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!";
    }

}

My Custom 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));
            }
        }
}

Thank you guys!

Try setting the Source property of your binding

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

By default bindings use the .DataContext as the Source for the binding, however in your case TestClass does not have its .DataContext set to anything. In fact, I'm not even sure if that's a valid property on DependencyObject .

Normally the .DataContext is inherited from the object's parent in WPF's visual tree, but since TestClass is not part of your visual tree, there's nothing for it to inherit from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM