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
Solved

WPF MVVM ListView Selected Item

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

789 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