Solved

WPF MVVM ListView Selected Item

Posted on 2014-10-05
4
1,592 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
  • 2
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
apeter earned 500 total points
Comment Utility
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
 

Author Comment

by:Alyanto
Comment Utility
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
 

Author Closing Comment

by:Alyanto
Comment Utility
Posed the cheeky question seperatly
0
 
LVL 25

Expert Comment

by:apeter
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now