Solved

WPF MVVM ListView Selected Item

Posted on 2014-10-05
4
1,845 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

635 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