2

I'm working on a modular application that will feature procedures that may or may not have parameters. Parameters are loaded to each procedure and on execution goal is to ask the user to input all required parameters then it does some extra work.

I've managed to load everything in and it all works fine, but I don't know how to make the dynamic binding for each of the parameters value.

I've made a demo application to test this, and even though I've fiddled with it for a bit I still can't seem to get it to work and I don't know what's missing.

Here's the code for the demo app that is a simplified version of the actual application, the concept is almost identical though:

public class TestBinding 
{
    public List<Val> Values {get;set;}

    public TestBinding()
    {
        Values = new List<Val>();
        Values.Add(new Val {Caption = "First", Value = String.Empty});
        Values.Add(new Val {Caption = "Second", Value = String.Empty});
        Values.Add(new Val {Caption = "Third", Value = String.Empty});
    }
}

public class Val 
{
    public string Caption {get;set;}
    public string Value {get;set;}
}

public TestBinding TB {get;set;}
public Window1()
{
    InitializeComponent();

    TB = new TestBinding();

    foreach(var x in TB.Values)
    {
        var txt = new TextBox() {Height = 25, Width = 150};

        var myBinding = new Binding("x.Value"); //???? Not sure about this
        myBinding.Source = x.Value;
        myBinding.Mode = BindingMode.TwoWay;
        myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        BindingOperations.SetBinding(txt, TextBox.TextProperty, myBinding);

        SPanel.Children.Add(txt);
    }

    var btn = new Button() {Height = 20, Width = 150, Content = "Show values"};

    btn.Click += new RoutedEventHandler(radioButton1_Click);
    SPanel.Children.Add(btn);
}

private void radioButton1_Click(object sender, RoutedEventArgs e)
{
    foreach(var x in TB.Values)
    {
        MessageBox.Show(x.Value);
    }
}

1 Answer 1

4

You have confused the Source object of the Binding and the Path to a source property of that object.

It should look like this:

var myBinding = new Binding("Value");
myBinding.Source = x;
myBinding.Mode = BindingMode.TwoWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

or

var myBinding = new Binding
{
    Path = new PropertyPath("Value"),
    Source = x,
    Mode = BindingMode.TwoWay,
    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
};

txt.SetBinding(TextBox.TextProperty, myBinding);
Sign up to request clarification or add additional context in comments.

3 Comments

Works like a charm! Thank you very much for the explanation as well! :)
Whoa, problem occured! If I change the value from code ( TB.Values[0].Value = "ABC") It stays changed but the change does not propagate to the UI element even though it's a Two way binding..
class Val should implement INotifyPropertyChanged and fire the PropertyChanged event when its Value property changes.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.