locked
How do I set the data binding for a control that refers to a property on a listbox's selected item? RRS feed

  • Question

  • Hello,

    I'm fairly new to WPF and Blend, and am trying to get my head around the excellent data binding it offers. However, I'm a bit stuck with what seems to be a fairly simple issue.

    Imagine a form with a listbox that is bound to a collection of Person objects. On the form are some other controls, and I want it so that when you click on a person in the listbox, the other controls will be populated with details for that person (first name, surname, age, etc).

    I added a Grid to the form, and set its data context to be the listbox's selected item. Now, as far as I understand it, all I should need to do now is set the Text property of a Textbox to the Person.FirstName property in the data context, and all should work.

    If I do this by using Blend's UI, the Textbox is bound to something like PersonCollection[0].FirstName, which is not right. This will always show the first name of the first person in the list, irrespective of what is selected.

    If I manually change the XAML so that the binding is just FirstName, then it works fine.

    Is there a way to do this using the UI instead of modifying the XAML by hand? Apart from the obvious ease of the UI, it makes mistakes far less likely.

    Thanks in advance for any help you can give.


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Thursday, January 27, 2011 11:18 PM

Answers

  • That is the List-details data binding in Blend.

    Search "Create a list-details data binding" in the Blend user guide, you will find the instruction.

    In Brief, bind the listbox in list mode, then switch from List mode to Detail Mode on the top of datapane, then drag the data node to artboard, it will create the controls that bind to the seleted item.


    Thanks, Harry [MSFT]
    • Marked as answer by Mr Yossu Tuesday, February 1, 2011 3:49 PM
    Saturday, January 29, 2011 9:34 AM

All replies

  • That is the List-details data binding in Blend.

    Search "Create a list-details data binding" in the Blend user guide, you will find the instruction.

    In Brief, bind the listbox in list mode, then switch from List mode to Detail Mode on the top of datapane, then drag the data node to artboard, it will create the controls that bind to the seleted item.


    Thanks, Harry [MSFT]
    • Marked as answer by Mr Yossu Tuesday, February 1, 2011 3:49 PM
    Saturday, January 29, 2011 9:34 AM
  • Hello.

    Thanks for the reply. Sorry it's taken a couple of days to reply, but I was having serious issue with controls not being displayed, and I could check this out.

    Anyway, the article you mentioned looks like it will explain it all clearly. I can see I need to spend more time in that User Guide!

    Thanks again


    If you're really bored, you could read about my experiments with .NET and some of Microsoft's newer technologies at http://dotnetwhatnot.pixata.co.uk/
    Tuesday, February 1, 2011 3:49 PM