0

I have a class structure as follows :

public class Person
{
    public PersonInfo Details { get; set; }
    public string Name { get; set; }
}

public class PersonInfo
{
    public string Designation { get; set; }
}

I am trying bind 2 textBoxes as follows:

Person person = new Person();

textBox1.DataContext = person;
textBox2.DataContext = person;

Binding textBox1Binding = new Binding()
{
   Mode = BindingMode.OneWayToSource,
   Path = new PropertyPath("Name"),
};

Binding textBox1Binding = new Binding()
{
   Mode = BindingMode.OneWayToSource,
   **Path = new PropertyPath("Details.Designation")**   << problem is here
};

How to bind "Details.Designation" to textbox2 ?

Any help would be appreciated.

1 Answer 1

4

It took some time, but I think I have a solution. Your PersonInfo-Object in Person does not exist. I modified your code to the following and now it works for me

  public class Person
  {
    private PersonInfo pi;

    public Person()
    {
      pi = new PersonInfo();
    }

    public PersonInfo Details
    {
      get
      {
        return pi;
      }
      set
      {
        pi = value;
      }
    }
    public string Name { get; set; }
  }

  public class PersonInfo
  {
    public string Designation { get; set; }
  }

And somewhere:

  textBox1.DataContext = person;
  textBox2.DataContext = person;

  Binding textBox1Binding = new Binding()
  {
    Mode = BindingMode.OneWayToSource,
    Path = new PropertyPath("Name"),
  };
  textBox1.SetBinding(TextBox.TextProperty, textBox1Binding);

  Binding textBox2Binding = new Binding()
  {
    Mode = BindingMode.OneWayToSource,
    Path = new PropertyPath("Details.Designation"),
  };
  textBox2.SetBinding(TextBox.TextProperty, textBox2Binding);

Can you ensure that your Sub-Object 'PersonInfo' (via Property Designation) exists before binding?

Sign up to request clarification or add additional context in comments.

Comments

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.