Solved

WPF MVVM ListView Selected Item

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 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