Intersoft ClientUI 8 > ClientUI Fundamentals > MVVM Pattern Overview > MVVM Pattern Walkthroughs > Walkthrough: Create Linked Input on Two UXComboBox using MVVM Pattern |
This walkthrough shows you how to use MVVM pattern to create a linked UXComboBox and show detailed info for the selected item. This walkthrough demonstrates the following concept:
You need the following components to complete this walkthrough:
The first step is to create a new ClientUI MVVM Application project using Intersoft ClientUI MVVM Application project template in Visual Studio.
This section shows how to create a Model class in Font.cs and FontWeight.cs. The model will map each information in the data entity to a property.
C# |
Copy Code
|
---|---|
private string _name; private string _price; public string Name { get { return _name; } set { if (_name != value) { _name = value; OnPropertyChanged("Name"); } } } public string Price { get { return _price; } set { if (_price != value) { _price = value; OnPropertyChanged("Price"); } } } |
C# |
Copy Code
|
---|---|
private string _name; private ObservableCollection<FontWeight> _fontWeights; public string Name { get { return _name; } set { if (_name != value) { _name = value; OnPropertyChanged("Name"); } } } public ObservableCollection<FontWeight> FontWeights { get { return _fontWeights; } set { if (_fontWeights != value) { _fontWeights = value; OnPropertyChanged("FontWeights"); } } } |
C# |
Copy Code
|
---|---|
public Font(XElement h) { Name = h.Element("Name").Value.ToString(); FontWeights = new ObservableCollection<FontWeight>(); foreach (XElement fontWeight in h.Descendants("FontWeight")) { FontWeights.Add(new FontWeight() { Name = fontWeight.Element("Name").Value.ToString(), Price = fontWeight.Element("Price").Value.ToString() }); } } |
This section shows how to create a View pattern used in the application interface using LinkCombo.xaml. We bind the data to the control in later section
For more information on how to add a new item in Visual Studio, see Walkthrough: Add New Item such as Page, Dialog Box and Window in VS 2010 |
Property | Value |
---|---|
Width | Auto |
Height | Auto |
Orientation | Vertical |
This section shows how to create a ViewModel to hold the collection of Font and FontWeight model object. There is also properties to hold the UXComboBox SelectedItem and selection summary.
C# |
Copy Code
|
---|---|
private ObservableCollection<Font> _fonts; public ObservableCollection<Font> Fonts { get { return _fonts; } set { if (_fonts != value) { _fonts = value; OnPropertyChanged("Fonts"); } } } |
C# |
Copy Code
|
---|---|
private Font _selectedFont; public Font SelectedFont { get { return _selectedFont; } set { if (_selectedFont != value) { _selectedFont = value; OnPropertyChanged("SelectedFont"); } } } |
C# |
Copy Code
|
---|---|
private string _priceInfo; public string PriceInfo { get { return _priceInfo; } set { if (_priceInfo != value) { _priceInfo = value; OnPropertyChanged("PriceInfo"); } } } |
C# |
Copy Code
|
---|---|
public LinkComboViewModel() { LoadData(); } public void LoadData() { StreamResourceInfo resource = System.Windows.Application.GetResourceStream( new Uri("LinkUXComboBoxMVVM;component/Data/FontDataSource.xml",UriKind.Relative)); XDocument doc = XDocument.Load(resource.Stream); Fonts = new ObservableCollection<Font>(); foreach(XElement font in doc.Descendants("Font")) { Fonts.Add(new Font(font)); } resource.Stream.Close(); } |
C# |
Copy Code
|
---|---|
public Font SelectedFont { get { return _selectedFont; } set { if (_selectedFont != value) { _selectedFont = value; if (_selectedFont != null) FontWeights = _selectedFont.FontWeights; PriceInfo = ""; OnPropertyChanged("SelectedFont"); } } } |
C# |
Copy Code
|
---|---|
public FontWeight SelectedFontWeight { get { return _selectedFontWeight; } set { if (_selectedFontWeight != value) { _selectedFontWeight = value; PriceInfo = string.Format("{0} {1} {2}", SelectedFont.Name, _selectedFontWeight.Name, _selectedFontWeight.Price); OnPropertyChanged("SelectedFontWeight"); } } } |
This section shows how to bind the ViewModel property object to the LinkCombo page.
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage ... xmlns:ViewModels="clr-namespace:LinkUXComboBoxMVVM.ViewModels" ... > ... </Intersoft:UXPage> |
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage.DataContext> <ViewModels:LinkComboViewModel /> </Intersoft:UXPage.DataContext> |
XAML |
Copy Code
|
---|---|
<Intersoft:UXComboBox Name="uXComboBox1" Width="175" ItemsSource="{Binding Path=Fonts}" DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedFont, Mode=TwoWay}" /> |
XAML |
Copy Code
|
---|---|
<Intersoft:UXComboBox Name="uXComboBox2" Width="175" SelectedItem="{Binding Path=SelectedFontWeight, Mode=TwoWay}" DisplayMemberPath="Name" ItemsSource="{Binding Path=FontWeights}" /> |
In order to view the result, you will need to build the Silverlight project and run the test page in browser.
In this walkthrough, you have learned how to create ClientUI MVVM Application project using Intersoft ClientUI MVVM Application project template, and create classes with page based on the MVVM pattern. You also learned how to linked two UXComboBox.
For more information about application development using MVVM pattern, see MVVM Pattern Overview
This section lists the complete code used in this walkthrough.
C# |
Copy Code
|
---|---|
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using LinkUXComboBoxMVVM.ViewModels; namespace LinkUXComboBoxMVVM.Models { public class FontWeight: ModelBase { private string _name; private string _price; public string Name { get { return _name; } set { if (_name != value) { _name = value; OnPropertyChanged("Name"); } } } public string Price { get { return _price; } set { if (_price != value) { _price = value; OnPropertyChanged("Price"); } } } public FontWeight() { } } } |
C# |
Copy Code
|
---|---|
using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using LinkUXComboBoxMVVM.ViewModels; using System.Xml.Linq; using System.Collections.ObjectModel; namespace LinkUXComboBoxMVVM.Models { public class Font: ModelBase { private string _name; private ObservableCollection<FontWeight> _fontWeights; public string Name { get { return _name; } set { if (_name != value) { _name = value; OnPropertyChanged("Name"); } } } public ObservableCollection<FontWeight> FontWeights { get { return _fontWeights; } set { if (_fontWeights != value) { _fontWeights = value; OnPropertyChanged("FontWeights"); } } } public Font(XElement h) { Name = h.Element("Name").Value.ToString(); FontWeights = new ObservableCollection<FontWeight>(); foreach (XElement fontWeight in h.Descendants("FontWeight")) { FontWeights.Add(new FontWeight() { Name = fontWeight.Element("Name").Value.ToString(), Price = fontWeight.Element("Price").Value.ToString() }); } } } } |
C# |
Copy Code
|
---|---|
using System; using System.Net; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Intersoft.Client.Framework.Input; using System.Collections.ObjectModel; using LinkUXComboBoxMVVM.Models; using System.Windows.Resources; using System.Xml.Linq; namespace LinkUXComboBoxMVVM.ViewModels { public class LinkComboViewModel : ViewModelBase { private ObservableCollection<Font> _fonts; private ObservableCollection<FontWeight> _fontWeights; private Font _selectedFont; private FontWeight _selectedFontWeight; private string _priceInfo; public ObservableCollection<Font> Fonts { get { return _fonts; } set { if (_fonts != value) { _fonts = value; OnPropertyChanged("Fonts"); } } } public ObservableCollection<FontWeight> FontWeights { get { return _fontWeights; } set { if (_fontWeights != value) { _fontWeights = value; OnPropertyChanged("FontWeights"); } } } public Font SelectedFont { get { return _selectedFont; } set { if (_selectedFont != value) { _selectedFont = value; if (_selectedFont != null) FontWeights = _selectedFont.FontWeights; PriceInfo = ""; OnPropertyChanged("SelectedFont"); } } } public FontWeight SelectedFontWeight { get { return _selectedFontWeight; } set { if (_selectedFontWeight != value) { _selectedFontWeight = value; PriceInfo = string.Format("{0} {1} {2}", SelectedFont.Name, _selectedFontWeight.Name, _selectedFontWeight.Price); OnPropertyChanged("SelectedFontWeight"); } } } public string PriceInfo { get { return _priceInfo; } set { if (_priceInfo != value) { _priceInfo = value; OnPropertyChanged("PriceInfo"); } } } // Constructors public LinkComboViewModel() { LoadData(); } public void LoadData() { StreamResourceInfo resource = System.Windows.Application.GetResourceStream( new Uri("LinkUXComboBoxMVVM;component/Data/FontDataSource.xml",UriKind.Relative)); XDocument doc = XDocument.Load(resource.Stream); Fonts = new ObservableCollection<Font>(); foreach(XElement font in doc.Descendants("Font")) { Fonts.Add(new Font(font)); } resource.Stream.Close(); } } } |
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Intersoft="http://intersoft.clientui.com/schemas" xmlns:ViewModels="clr-namespace:LinkUXComboBoxMVVM.ViewModels" mc:Ignorable="d" x:Class="LinkUXComboBoxMVVM.Views.LinkCombo" Title="LinkCombo Page" d:DesignWidth="640" d:DesignHeight="480"> <Intersoft:UXPage.DataContext> <ViewModels:LinkComboViewModel /> </Intersoft:UXPage.DataContext> <Grid x:Name="LayoutRoot"> <Intersoft:UXStackPanel HorizontalAlignment="Left" Margin="10,10,0,0" Name="uXStackPanel1" VerticalAlignment="Top" Orientation="Vertical"> <Intersoft:FieldLabel Header="Select Font: " Name="fieldLabel1" HeaderWidth="125"> <Intersoft:UXComboBox Name="uXComboBox1" Width="175" ItemsSource="{Binding Path=Fonts}" DisplayMemberPath="Name" SelectedItem="{Binding Path=SelectedFont, Mode=TwoWay}" /> </Intersoft:FieldLabel> <Intersoft:FieldLabel Header="Select FontWeight: " Name="fieldLabel2" HeaderWidth="125"> <Intersoft:UXComboBox Name="uXComboBox2" Width="175" SelectedItem="{Binding Path=SelectedFontWeight, Mode=TwoWay}" DisplayMemberPath="Name" ItemsSource="{Binding Path=FontWeights}" /> </Intersoft:FieldLabel> <TextBlock Height="23" Name="textBlock1" Text="{Binding Path=PriceInfo}" /> </Intersoft:UXStackPanel> </Grid> </Intersoft:UXPage> |