3

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!

2
  • can u explain what are you tryingto do in detail ?
    – Muds
    Apr 9, 2015 at 15:57
  • I want to stay internal variables the same. Example: I have an object of the class Foo which has some properties and I want to bind a certain property of this object to a certain property of my class Bar. Apr 9, 2015 at 16:00

1 Answer 1

4

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.

2
  • @MisterPresident No problem, glad that worked for you :)
    – Rachel
    Apr 9, 2015 at 19:37
  • MisterPresident accept it as answer, so that community can be benefitted.
    – Muds
    Apr 10, 2015 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.