views:

170

answers:

2

I am stuck on how to pass data from one control to another. If I have a listbox control and the Contol Item contains a datatemplate which renders out 5 fields ( first name, last name, email, phone and DOB) all of which come from an observable collection. How can I allow the user to select a listbox item and have the valuesbe stored within a new listbox control?

Is this done through the creation of a new collection or is there a more simple way to bind these values to a new control?

thank you,

A: 

Let's say you have a:

<ListBox Name="DemoList" ItemsSource="{Binding ...}">
    <ListBox.ItemTemplate>
        ...
    </ListBox.ItemTemplate>
</ListBox>

And another control, maybe a TextBox:

<TextBox Text="I want to bind this to the Email property" />

You can achieve this pretty easily, with:

<TextBox Text="{Binding ElementName=DemoList, Path=SelectedItem.Email}" />

Note the ElementName property of the Binding. This allows you to bind relative to another control, and in this case you want the SelectedItem of your ListBox. SelectedItem will contain an element of the collection in the ItemsSource (or null if nothing is selected), so you can then bind to its properties.

It gets more complex if you want to support multiple selection, but it doesn't sound like this is a requirement for you.

JustABill
unfortuantly... mutiple selection is a criteria. I can see the data being captured in memory so I have the selectedItem piece correct I am just unsure as to how to pass this to a new collection which would allow me to select additional items.thank you all for the responses thus far!
randyc
I've never actually tried this, but you could do something like: <ListBox ... SelectedItems="{Binding Path=TargetPath, Mode=OneWayToSource}"> which in theory would cause the ListBox to send its SelectedItems collection to your class. I'm not sure what type the target collection should be, though. You could also manually create a Binding in your code to link the two.
JustABill
A: 

If it is not too late, I would strongly recommend that you use the MVVM pattern. The problem you are facing is typical for WPF without a decent presentation model and wont be the last one.

Using MVVM you would pass data between controls/views through the ViewModel. In your example you would have a PersonViewModel with an ObservableCollection containing first name, last name, email and DOB. Additionally it would have a property SelectedItem. This property can be bound to in a lot of different controls/views without them having to know each other.

bitbonk
I do have my collection in a separate model labeled services. This class is part of a separate assembly that I referecnce in my project. The grid vew is bound to the collection and when the item is selected I can see the data I just can't figure out how to pass it to either a new collection. I don't what to bind the data directly to the second control through elementName and path as I need a new list item created when another listbox item is selected. This is why I think I need to create a new collection and add the data that is selected to this new collection ( almost like an array i guess.
randyc