Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

WPF MVVM ListView Selected Item

Posted on 2014-10-05
4
Medium Priority
?
1,943 Views
Last Modified: 2014-10-07
I am fairly new to MVVM and WPF and I am trying to get two bits of information about listbox and combobox.
When loading either with data I know about the DisplayMemberPath how do I add a hidden or value binding?

When selecting an item in either what event do I use to bind to and is there a good guide about doing this ?

<ComboBox x:Name="cboProductType" ItemsSource="{Binding ProductType}" DisplayMemberPath=" ProductTypeName" Grid.Column="1" Margin="111,72,348,164"/>

Open in new window

0
Comment
Question by:Alyanto
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
apeter earned 2000 total points
ID: 40363674
Please let me know what you mean by "hidden or value" ? you have a initial item before loading ?

"...What event do I use..."
xaml
--------
<ComboBox MaxHeight="25" ItemsSource="{Binding MySource}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />

ViewModel which have the source or data for the combobox
---------------------------------------------------------------------------------------------
List<string> _source = new List<string>{"Item 1", "Item 2", "Item 3"};
string _selectedItem = null;

Below property should be used in the xaml ((implement the INotifyPropertyChanged interface while setting the _source))
----------------------------------------------------------------
Public List<string> MySource { get { return _source; } }


property which holds the selected item
-------------------------------------------------------
public string SelectedItem { get { return _selectedItem; } set { _selectedItem = value; //NotifyPropertyChanged} }
0
 
LVL 1

Author Comment

by:Alyanto
ID: 40365001
I had not appreciated I was loading the object so the selecteditem binding solved the problem for me.  I come from a winforms background and you can have a display and a value item but in WPF that seems unnecessary.  

For the true hero status how do I get these items to refresh when new items are added to the observable collection?  A little cheeky I know but go to try it on :)
0
 
LVL 1

Author Closing Comment

by:Alyanto
ID: 40365040
Posed the cheeky question seperatly
0
 
LVL 25

Expert Comment

by:apeter
ID: 40365597
Can i have the link for that cheeky question please.... Here is the answer for cheeky question. If you have observerable collection as your "source" then adding and deletion will be take care automatically by the binding engine.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question