Intersoft ClientUI 8 > ClientUI Fundamentals > MVVM Pattern Overview > MVVM Pattern Walkthroughs > Walkthrough: Display Data Details of UXFlow’s SelectedItem using MVVM Pattern |
This walkthrough shows you how to use MVVM pattern to show detailed info for the selected item of UXFlow.
In this walkthrough, you perform the following tasks:
You need the following components to complete this walkthrough:
The first step is to create a new ClientUI MVVM Application using Intersoft ClientUI MVVM project template in Visual Studio.
Next, you will create the Hotel model class that represent the data entity used in this walkthrough.
This section shows you how to create the Hotel model class that represents the data entity used in this walkthrough. The Hotel model class contains constructors to obtain the URI path of the hotel's image.
Sample Code |
Copy Code
|
---|---|
private Uri _image; public Uri Image { get { return this._image; } set { if (this._image != value) { this._image = value; this.OnPropertyChanged("Image"); } } } |
Sample Code |
Copy Code
|
---|---|
public class Hotel : ViewModels.ModelBase { #region Fields private Uri _image; private string _location; private string _name; #endregion #region Properties public Uri Image { get { return this._image; } set { if (this._image != value) { this._image = value; this.OnPropertyChanged("Image"); } } } public string Location { get { return this._location; } set { if (this._location != value) { this._location = value; this.OnPropertyChanged("Location"); } } } public string Name { get { return this._name; } set { if (this._name != value) { this._name = value; this.OnPropertyChanged("Name"); } } } #endregion } |
Sample Code |
Copy Code
|
---|---|
public Hotel() { } public Hotel(XElement h) : this() { string image = h.Element("Image").Value.Trim(); this._image = new Uri("/UXListBox-Walkthrough;component/Images/" + image, UriKind.RelativeOrAbsolute); this._location = h.Element("Location").Value.Trim(); this._name = h.Element("Name").Value.Trim(); } |
This section show how to create a View used in the application interface. You will bind the data to the control in the 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 |
XAML |
Copy Code
|
---|---|
<Intersoft:DockPanel Name="dockPanel1" FillChildMode="Custom"> <Intersoft:UXFlow Height="480" Name="uXFlow1" Intersoft:DockPanel.Dock="Right" Intersoft:DockPanel.IsFillElement="True"></Intersoft:UXFlow> </Intersoft:DockPanel> |
Properties | Value |
---|---|
HorizontalAlignment | Center |
VerticalAlignment | Center |
DockPanel.Dock | Left |
Height | Auto |
XAML |
Copy Code
|
---|---|
<Intersoft:DockPanel Name="dockPanel1" FillChildMode="Custom"> ... <Intersoft:DockPanel Name="dockPanel2" Width="200" Intersoft:DockPanel.Dock="Left" HorizontalAlignment="Center" VerticalAlignment="Center" /> </Intersoft:DockPanel> |
Properties | Value |
---|---|
DockPanel.Dock | Top |
Width | 200 |
Content | Reset Value |
Properties | Value |
---|---|
DockPanel.Dock | Top |
HorizontalAlignment | Left |
Header | Rating: |
Properties | Value |
---|---|
DockPanel.Dock | Top |
HorizontalAlignment | Left |
Header | Rate: |
XAML |
Copy Code
|
---|---|
<Intersoft:DockPanel Name="dockPanel1" FillChildMode="Custom"> <Intersoft:UXFlow Height="480" Name="uXFlow1" Intersoft:DockPanel.Dock="Right" Intersoft:DockPanel.IsFillElement="True"></Intersoft:UXFlow> <Intersoft:DockPanel Name="dockPanel2" Width="200" Intersoft:DockPanel.Dock="Left" HorizontalAlignment="Center" VerticalAlignment="Center" Intersoft:DockPanel.IsFillElement="True"> <Intersoft:GlassLabel Name="glassLabel1" Intersoft:DockPanel.Dock="Top" Width="200"> <StackPanel Name="stackPanel1"> <TextBlock Name="textBlock1" Text="TextBlock" HorizontalAlignment="Center" /> <TextBlock Name="textBlock2" Text="TextBlock" HorizontalAlignment="Center" /> </StackPanel> </Intersoft:GlassLabel> <Intersoft:FieldLabel Header="Rating: " Name="fieldLabel1" Intersoft:DockPanel.Dock="Top" HorizontalAlignment="Left"> <Intersoft:UXTextBox HorizontalAlignment="Left" Name="uXTextBox1" Text="UXTextBlock" VerticalAlignment="Top" /> </Intersoft:FieldLabel> <Intersoft:FieldLabel Header="Rate: " Name="fieldLabel2" Intersoft:DockPanel.Dock="Top" HorizontalAlignment="Left"> <Intersoft:UXTextBox HorizontalAlignment="Left" Name="uXTextBox2" Text="UXTextBlock" VerticalAlignment="Top" /> </Intersoft:FieldLabel> </Intersoft:DockPanel> </Intersoft:DockPanel> |
This section steps you through the process of creating a ViewModel class that contains the properties to describe the View that you created in the previous section. The ViewModel defines the Hotels collection to represent an observable collection of Hotel model and a LoadData method to load hotel data from xml file.
Sample Code |
Copy Code
|
---|---|
public class HotelListViewModel : ViewModelBase |
Sample Code |
Copy Code
|
---|---|
public class HotelListViewModel : ViewModelBase { // Fields private Hotel _selectedItem = null; // Views public ObservableCollection<Hotel> Hotels { get; set; } // Selection, View States public Hotel SelectedItem { get { return _selectedItem; } set { if (_selectedItem != value) { _selectedItem = value; OnPropertyChanged("SelectedItem"); } } } // Constructor public HotelListViewModel() { this.LoadData(); } private void LoadData() { // loads hotel data from xml file StreamResourceInfo resource = System.Windows.Application.GetResourceStream( new Uri("UXListBox-Walkthrough;component/Data/HotelDataSource.xml", UriKind.Relative)); XDocument doc = XDocument.Load(resource.Stream); var hotels = from x in doc.Descendants("Hotel") select new Hotel(x); this.Hotels = new ObservableCollection<Hotel>(); foreach (Hotel hotel in hotels) { this.Hotels.Add(hotel); } resource.Stream.Close(); } } |
Next, you will bind the HotelListViewModel to your HotelList.xaml.
This section shows how to bind the ViewModel that was created in the previous section to the View, for example you will bind the ItemsSource of UXFlow to Hotels property of the HotelListViewModel.
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage ... > ... <Intersoft:UXPage.DataContext> <ViewModels:HotelListViewModel /> </Intersoft:UXPage.DataContext> ... </Intersoft:UXPage> |
Properties | Value |
---|---|
ImageMemberPath | Image |
SubTitleMemberPath | Location |
TitleMemberPath | Name |
XAML |
Copy Code
|
---|---|
<Intersoft:DockPanel Name="dockPanel1" FillChildMode="Custom" DataContext="{Binding}"> <Intersoft:UXFlow Height="480" Name="uXFlow1" Intersoft:DockPanel.Dock="Right" Intersoft:DockPanel.IsFillElement="True" ItemsSource="{Binding Path=Hotels, Mode=TwoWay}" SelectedItem="{Binding Path=SelectedItem}" ImageMemberPath="Image" SubTitleMemberPath="Location" TitleMemberPath="Name"></Intersoft:UXFlow> ... </Intersoft:DockPanel> |
XAML |
Copy Code
|
---|---|
<Intersoft:DockPanel Name="dockPanel2" Width="200" Intersoft:DockPanel.Dock="Left" HorizontalAlignment="Center" VerticalAlignment="Center" DataContext="{Binding Path=SelectedItem}"> <Intersoft:GlassLabel Name="glassLabel1" Intersoft:DockPanel.Dock="Top" Width="200"> ... </Intersoft:DockPanel> |
XAML |
Copy Code
|
---|---|
<StackPanel Name="stackPanel1"> <TextBlock Name="textBlock1" Text="{Binding Path=Name}" HorizontalAlignment="Center"/> ... </StackPanel> |
XAML |
Copy Code
|
---|---|
<StackPanel Name="stackPanel1"> ... <TextBlock Name="textBlock2" Text="{Binding Path=Location}" HorizontalAlignment="Center"/> </StackPanel> |
XAML |
Copy Code
|
---|---|
<Intersoft:FieldLabel Header="Rating: " ... > <Intersoft:UXTextBox HorizontalAlignment="Left" Name="uXTextBox1" Text="{Binding Path=Rating}" VerticalAlignment="Top" /> </Intersoft:FieldLabel> |
XAML |
Copy Code
|
---|---|
<Intersoft:FieldLabel Header="Rate: " ... > <Intersoft:UXTextBox HorizontalAlignment="Left" Name="uXTextBox2" Text="{Binding Path=Rate}" VerticalAlignment="Top" /> </Intersoft:FieldLabel> |
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 System.Xml.Linq; using CoverFlowMVVM.ViewModels; namespace CoverFlowMVVM.Models { public class Hotel: ModelBase { #region Constructors public Hotel(XElement h) { string image = h.Element("Image").Value.Trim(); this._image = new Uri("/CoverFlowMVVM;component/Assets/Images/" + image, UriKind.RelativeOrAbsolute); this._location = h.Element("Location").Value.Trim(); this._name = h.Element("Name").Value.Trim(); this._rate = h.Element("Rate").Value.Trim(); this._rating = h.Element("Rating").Value.Trim(); } #endregion #region Fields private Uri _image; private string _location; private string _name; private string _rate; private string _rating; #endregion #region Properties public Uri Image { get { return this._image; } set { if (this._image != value) { this._image = value; this.OnPropertyChanged("Image"); } } } public string Location { get { return this._location; } set { if (this._location != value) { this._location = value; this.OnPropertyChanged("Location"); } } } public string Name { get { return this._name; } set { if (this._name != value) { this._name = value; this.OnPropertyChanged("Name"); } } } public string Rate { get { return this._rate; } set { if (this._rate != value) { this._rate = value; this.OnPropertyChanged("Rate"); } } } public string Rating { get { return this._rating; } set { if (this._rating != value) { this._rating = value; this.OnPropertyChanged("Rating"); } } } #endregion } } |
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:CoverFlowMVVM.ViewModels" mc:Ignorable="d" x:Class="CoverFlowMVVM.Views.HotelList" Title="HotelList Page" d:DesignWidth="640" d:DesignHeight="480"> <Intersoft:UXPage.DataContext> <ViewModels:HotelListViewModel /> </Intersoft:UXPage.DataContext> <Grid x:Name="LayoutRoot"> <Intersoft:DockPanel Name="dockPanel1" FillChildMode="Custom"> <Intersoft:UXFlow Height="480" Name="uXFlow1" Intersoft:DockPanel.Dock="Right" Intersoft:DockPanel.IsFillElement="True" ItemsSource="{Binding Path=Hotels, Mode=TwoWay}" ImageMemberPath="Image" SubTitleMemberPath="Location" TitleMemberPath="Name" SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"></Intersoft:UXFlow> <Intersoft:DockPanel Name="dockPanel2" Width="200" Intersoft:DockPanel.Dock="Left" HorizontalAlignment="Center" VerticalAlignment="Center" DataContext="{Binding Path=SelectedItem}" Intersoft:DockPanel.IsFillElement="True"> <Intersoft:GlassLabel Name="glassLabel1" Intersoft:DockPanel.Dock="Top" Width="200"> <StackPanel Name="stackPanel1"> <TextBlock Name="textBlock1" Text="{Binding Path=Name}" HorizontalAlignment="Center" /> <TextBlock Name="textBlock2" Text="{Binding Path=Location}" HorizontalAlignment="Center" /> </StackPanel> </Intersoft:GlassLabel> <Intersoft:FieldLabel Header="Rating: " Name="fieldLabel1" Intersoft:DockPanel.Dock="Top" HorizontalAlignment="Left"> <Intersoft:UXTextBox HorizontalAlignment="Left" Name="uXTextBox1" Text="{Binding Path=Rating}" VerticalAlignment="Top" /> </Intersoft:FieldLabel> <Intersoft:FieldLabel Header="Rate: " Name="fieldLabel2" Intersoft:DockPanel.Dock="Top" HorizontalAlignment="Left"> <Intersoft:UXTextBox HorizontalAlignment="Left" Name="uXTextBox2" Text="{Binding Path=Rate}" VerticalAlignment="Top" /> </Intersoft:FieldLabel> </Intersoft:DockPanel> </Intersoft:DockPanel> </Grid> </Intersoft:UXPage> |
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 Intersoft.Client.Framework.Input; using System.Collections.ObjectModel; using CoverFlowMVVM.Models; using System.Windows.Resources; using System.Xml.Linq; using System.Linq; namespace CoverFlowMVVM.ViewModels { public class HotelListViewModel : ViewModelBase { // Fields private Hotel _selectedItem = null; // Views public ObservableCollection<Hotel> Hotels { get; set; } // Selection, View States public Hotel SelectedItem { get { return _selectedItem; } set { if (_selectedItem != value) { _selectedItem = value; OnPropertyChanged("SelectedItem"); } } } // Constructor public HotelListViewModel() { this.LoadData(); } private void LoadData() { // loads hotel data from xml file StreamResourceInfo resource = System.Windows.Application.GetResourceStream( new Uri("UXListBox-Walkthrough;component/Data/HotelDataSource.xml", UriKind.Relative)); XDocument doc = XDocument.Load(resource.Stream); var hotels = from x in doc.Descendants("Hotel") select new Hotel(x); this.Hotels = new ObservableCollection<Hotel>(); foreach (Hotel hotel in hotels) { this.Hotels.Add(hotel); } resource.Stream.Close(); } } } |