I tried to link TextBox to my Rod class, but changing the TextBox.Text will not change the Rod fields. Whats wrong?

Viewed 42

I am dynamically creating rows using a Grid, which is a nested GroupBox, which has these custom TextBoxes. I bind it to my Rod class, but tried to change the fields of the TextBox.Text does not change the fields of Rod

    public RodTextBox(string text,string propertyName, Rod rod)
    {
        Text = text;
        TextWrapping = TextWrapping.Wrap;
        addBinding(propertyName ,rod);
    }
    public void addBinding(string propertyName, Rod rod )
    {
        Binding myBinding = new Binding($"{propertyName}");
        myBinding.Source = rod;
        myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        myBinding.Mode = BindingMode.TwoWay;
        SetBinding(TextBlock.TextProperty, myBinding);
    }
   
 }
1 Answers

We would need to see your definition of Rod, and of course your call to RodTextBox.

Just guessing though, maybe the property in Rod you are binding to doesn't have {get; set;}?

In Visual Studio, you can also enable WPF binding errors (tools->options->Debugging->WPF Trace Settings->DataBinding to All) and see if the console shows any binding errors.

Related