How to pass data between controls and persist the values in WPF

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,

-------------Problems Reply------------

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.

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.

Category:wpf Views:1 Time:2010-05-27

Related post

  • Passing data to server code from a client-side control using ASP.NET AJAX 2008-10-30

    I have a ListView on a page that displays a list of widgets. When a user clicks on one of the items in the list, I want to display a ModalPopup that contains controls allowing the user to operate on the item they selected. I could easily accomplish t

  • The correct pattern for passing data to child controls in a server control 2009-07-10

    I'm working with a 3rd party system to implement some forms in a website. The 3rd party system provides me with XML definitions for these forms. e.g. <form> <segment> <label>The header</label> <fields> ... <field>

  • Zend passing data from action in controller to view in another controler 2010-12-14

    How can I pass data from action in controller to view in another controller? From action in controler to view in the same controller is easy: I simply write in action's function : $this->view->assign('error', 'Wrong login'); and in view I recie

  • Passing Data to another user control 2011-08-31

    Setup: I have an .aspx file with 3 user controls housed inside a RadAjaxPanel and RadPanelBar. What I have constructed is a workflow type of form allowing a user to add a new db record based on a template of an existing record that was retrieved from

  • Pass Data into User Control on Master Page from Sub Page 2012-03-09

    I have a user control on the master page and I would like to pass in a value into that user control from the subpage, how would I be able to pass the values? This control is in the master page <%@ Register TagPrefix="test" TagName="Data" Src="cont

  • Passing data from one User Control to another in WPF 2012-04-16

    I am new to WPF , I am working on a project which involves communication between two user controls. I know the it can done using Delegates and events and i Surfed in net regarding this and found the people are using MVVM to achieve this. Which is bet

  • How can I pass data from Perl to Java? 2009-03-04

    I'm working on some Java <-> Perl interaction. I would like to know what the best way is to pass information from Perl to Java. (Great answers about Perl and Java here and here btw). There's a lot of text and XML(XML::Twig) I'm parsing in Perl,

  • ASP.NET Passing Data Between Multiple Pages Session Question 2011-02-25

    So there seems not be any pretty answer to the question of how pass data between multiple pages. After having done a little homework here's why (or at least what I've gleaned): ViewState variables don't persist across pages. Session variables are vol

  • Passing Data Between ASP.net Pages using vb.net 2011-11-25

    I am writing my very first asp.net application and am trying to use the PreviousPage property to access data between pages. Visual 2010 is giving me an error which I don't understand, so I need some help to understand what I am doing wrong. I have an

  • .NET MVC and securely passing data between pages 2009-06-03

    a little background info first. I have been using Web Forms for most of my career and have recently become interested in .NET MVC. I realize that it is intended to be stateless, however, i dont understand how it is completely feasible. In a forms app

  • Passing data from WPF tabs to a function 2009-06-15

    I have a WPF app with a single form containing some WPF tabs. Three of the tabs are used to collect data from the user. The last tab has an OK button and when clicked performs some calculations. How to I pass data from the text boxes on the various t

  • ASP.NET MVC Passing Data from View to Controller 2009-06-18

    I have a view with a grid that contains items added to a workstation. The user can select an item from a drop down list and click an action link which calls a controller that adds that item to the workstation. I can make it work by reading the FormCo

  • Passing data into a ModalPopupExtender 2009-08-13

    I have a ModalPopupExtender that allows a customer to apply payment info. It has been working great. Then the customer asked to see the total due on the ModalPopup. This did not seem like a big deal to just take the total due from the parent control

  • Pass data to the view. Always through viewData ? 2009-08-24

    I’m building an application that monitors other systems. Now I want to implement partial view, a User Control called “status”. This control shall display status information about the application.Like: user logged in, How many systems online, Latest a

  • Passing Data to Custom Windows Service 2009-09-01

    I am developing a custom Windows service with C# that will manage state for several instances .NET applications. How can I pass data (such as token) to the service through the ServiceController? Is this possible or am I not fully understanding the de

  • Pass data from page to page safely 2009-11-20

    I am looking forward for a method to pass data from page to page safely and avoid as It's possible the tampering. The best way to solve it, is to save the sensitive data on db server. Or using session persist on db server. Or whatever method that per

  • Passing Data from Controller to View, back to Controller, back to View 2009-11-28

    I'm new to ASP.NET MVC. After working with traditional ASP.NET model for so long, it's taking some time for me to get to understand this model. I am going through NerdDinner to understand how things work. So, I have an object that needs to be passed

  • How to pass data between BLL and UI in 3-layer (single tier) application? 2009-12-04

    I am a fairly rookie programmer who is trying to learn the basics of n-layered architecture (DAL, BLL, UI). The application I am programming is a single tier, 3-layer application written in VB.NET (.Net 3.5). Layers as follows: DAL BLL UI COMMON - co

  • What is the preferred method of passing data between a service and an application 2009-12-15

    Possible Duplicate: Delphi 2009: How to communicate between Windows service & desktop application under Vista? I have a server running as a Windows service. To control the service and to display it's state I have an application running as a tray

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.135 (s). 11 q(s)