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

MisterPresident

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!

Rachel

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How to bind a property of control to another control's property

From Dev

How to bind a property of control to another control's property

From Dev

How to Bind to Another Control Property in WPF

From Dev

Bind a property to another property of a custom control

From Dev

How to Bind an another Control on the Setter Value Property by using a Trigger?

From Dev

How to bind user control property to other property?

From Dev

How to bind control to object property?

From Dev

How to bind control to object property?

From Dev

bind child user control to class property

From Dev

bind a value having dependency property from one class to another class textbox control in wpf

From Dev

How to bind a textbox to class Property

From Dev

bind dependency property from custom class to UI

From Dev

JavaFX Bind UI to a nested class's property

From Dev

Bind control to escaped property?

From Dev

How to bind data to a control's Visibility property

From Dev

How to bind data to a control's Visibility property

From Dev

How to parse a json object property to bind it to a control

From Dev

How I can bind property, which use another static property

From Dev

How to bind a property in the view model of a usercontrol to a property in another usercontrol

From Dev

How to bind to a control's property which is inside control template?

From Dev

How to bind Textbox text with other class property?

From Dev

How to use class property in another class ?

From Dev

JavaFX bind not property member to control

From Dev

Swift Class Property Of Another Property

From Dev

JavaFX - bind property from another class onto a status bar

From Dev

trying to bind datagrid item source to a property in another class

From Dev

Is it possible to bind the property of a datacontext to the property of another datacontext

From Dev

Is it possible to bind the property of a datacontext to the property of another datacontext

Related Related

  1. 1

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

  2. 2

    How to bind a property of control to another control's property

  3. 3

    How to bind a property of control to another control's property

  4. 4

    How to Bind to Another Control Property in WPF

  5. 5

    Bind a property to another property of a custom control

  6. 6

    How to Bind an another Control on the Setter Value Property by using a Trigger?

  7. 7

    How to bind user control property to other property?

  8. 8

    How to bind control to object property?

  9. 9

    How to bind control to object property?

  10. 10

    bind child user control to class property

  11. 11

    bind a value having dependency property from one class to another class textbox control in wpf

  12. 12

    How to bind a textbox to class Property

  13. 13

    bind dependency property from custom class to UI

  14. 14

    JavaFX Bind UI to a nested class's property

  15. 15

    Bind control to escaped property?

  16. 16

    How to bind data to a control's Visibility property

  17. 17

    How to bind data to a control's Visibility property

  18. 18

    How to parse a json object property to bind it to a control

  19. 19

    How I can bind property, which use another static property

  20. 20

    How to bind a property in the view model of a usercontrol to a property in another usercontrol

  21. 21

    How to bind to a control's property which is inside control template?

  22. 22

    How to bind Textbox text with other class property?

  23. 23

    How to use class property in another class ?

  24. 24

    JavaFX bind not property member to control

  25. 25

    Swift Class Property Of Another Property

  26. 26

    JavaFX - bind property from another class onto a status bar

  27. 27

    trying to bind datagrid item source to a property in another class

  28. 28

    Is it possible to bind the property of a datacontext to the property of another datacontext

  29. 29

    Is it possible to bind the property of a datacontext to the property of another datacontext

HotTag

Archive