Article Overview:
This demo shows how to get the selected item in the drop-down list in WPF's MVVM framework through data binding. The effect of the program after running is as follows:
Related Downloads (code, screen recording): http://pan.baidu.com/s/1sjwN357
Play Online: http://v.youku.com/v_show/id_XODA5OTYzMDU2.html
Warm tip : If the screen recording and code can not be downloaded, you can leave a message in the station, or send an email to [email protected]
The XAML code looks like this:
<window x:class= "Demo02ex01.mainwindow" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" title= "MainWindow" height= "262" width= "402" > <grid > <Grid.RowDefinitions> <rowdefinition/> <rowdefinition/> </ grid.rowdefinitions> <Grid.ColumnDefinitions> <columndefinition/> </grid.colum ndefinitions> <combobox grid.row= "0" grid.column= "0" width= "height=" "itemssource=" {B Inding companynames} "displaymemberpath=" CompanyName "selecteditem=" {Binding currentcompany} "/> <button grid.row= "1" width= "three" height= "content=" Get Selection "command=" {Binding Path=showselectedcompan Ycommand} "/> </Grid></Window>
The CS code looks like this:
Public partial class mainwindow:window{public MainWindow () { InitializeComponent (); This. DataContext = new Mainwindowmodel ();} }
Combining the above-mentioned code shows that the ViewModel class that is managed by the MainWindow view is the Mainwindowmodel class, and the member in the XAML code that refers to the binding is the member of the ViewModel class. For example, Companynames and Currentcompany are Mainwindowmodel properties.
The code for the Mainviewmodel class is as follows:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Windows;namespace demo02ex01.viewmodels{public class Mainwindowmodel {public Mainwindowmodel () {this. Companynames = new list<company> (); This. Companynames.add (new company () {CompanyName = "China Nuclear Industry Corporation", Address = "}"); This. Companynames.add (new company () {CompanyName = "Air China Technology Group", Address = "}"); This. Companynames.add (new company () {CompanyName = "China Power Technology Corporation", Address = "}"); This. Companynames.add (new company () {CompanyName = "China Three Gorges Project Hair corporation", Address = "}"); This. Companynames.add (new company () {CompanyName = "China Mobile Communications Corporation", Address = "}"); This. Currentcompany = this. COMPANYNAMES[1]; This. Showselectedcompanycommand = new Delegatecommand (this. Showselectedcompanyhandler); Public list<company> companynames {get; set;} Public company Currentcompany {get; set;} Public Delegatecommand Showselectedcompanycommand {get; set;} private void Showselectedcompanyhandler (object sender, Delegatecommandeventargs e) {MessageBox.Show (thi S.currentcompany.companyname); } }}
You can tell the program what to display by specifying the DisplayMemberPath property for the ComboBox. Because the type of companynames is list<company>, the string specified by DisplayMemberPath is the CompanyName property name of the company class. The code is as follows:
public class company{Public string CompanyName {get; set;} public string Address {get; set;}}
While the click on the button is handled by the command specified by the Commands property, it is still associated with the Mainwindowmodel member Showselectedcompanycommand. This is a custom command, and for the current button, the code that executes is as follows:
private void Showselectedcompanyhandler (object sender, Delegatecommandeventargs e) { MessageBox.Show (this. Currentcompany.companyname);}
At this point, the Currentcompany property of ViewModel can either modify the selected item in the drop-down list or get the selected item in the drop-down list in ViewModel.
Gets the selected item in the drop-down selection list in the MVVM framework of WPF