Intersoft ClientUI 8 > ClientUI Fundamentals > MVVM Pattern Overview > MVVM Pattern Walkthroughs > Walkthrough: Display Item Details in UXFrame using MVVM Pattern |
This walkthrough shows you how to use a UXFrame to list and show item detail with MVVM pattern. This walkthrough demonstrates the following concept:
You need the following components to complete this walkthrough:
In addition, you need the following resources to complete this walkthrough:
The assembly is required for the XML data source and images |
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 Book model class in Book.cs. The model will map each information in the data entity to a property.
C# |
Copy Code
|
---|---|
private string _author; public string Author { get { return _author; } set { if (_author != value) { _author = value; OnPropertyChanged("Author"); } } } |
C# |
Copy Code
|
---|---|
public Uri NavigateUri { get { return new Uri("/Books/" + this.ID, UriKind.RelativeOrAbsolute); } } |
C# |
Copy Code
|
---|---|
public Book(XElement x) : this() { this._author = x.Element("Author").Value.Trim(); this._title = x.Element("Title").Value.Trim(); this._category = x.Element("Category").Value.Trim(); this._ID = x.Element("ID").Value.Trim(); this._price = double.Parse(x.Element("Price").Value.Trim()); this._image = new Uri("/Intersoft.ClientUI.Samples.Assets;component/Images/Books/" + x.Element("Image").Value.Trim(), UriKind.RelativeOrAbsolute); } |
This section shows how to create a ViewModel to hold the collection of Book model object.
C# |
Copy Code
|
---|---|
public ObservableCollection<Book> FilteredBooks{ get; set; } |
C# |
Copy Code
|
---|---|
public BookListViewModel() { this.LoadBooks(); } private void LoadBooks() { // loads book data from xml file StreamResourceInfo resource = System.Windows.Application.GetResourceStream( new Uri("Intersoft.ClientUI.Samples.Assets;component/Data/BookDataSource.xml", UriKind.Relative)); XDocument doc = XDocument.Load(resource.Stream); var filteredBooks = (from x in doc.Descendants("Book") where x.Element("Category").Value.Trim() == "Computing and Internet" select new Book(x)).Take(8); FilteredBooks = new ObservableCollection<Book>(filteredBooks); } |
This section shows how to create three pages used for navigation purpose. The BookList.xaml is used to list all the Book object collection while the BookDetail.xaml is used to display detail information of a specific book. The BookContainer.xaml is used to navigate between the BookList and BookDetail view.
This section shows how to create a page which will list the Book collection in a WrapPanel, the page is using the BookList.xaml.
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
|
---|---|
xmlns:ViewModels="clr-namespace:ItemDetailUXFrame.ViewModels" |
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage.DataContext> <ViewModels:BookListViewModel/> </Intersoft:UXPage.DataContext> |
XAML |
Copy Code
|
---|---|
<Intersoft:UXItemsControl ...> <Intersoft:UXItemsControl.ItemsPanel> <ItemsPanelTemplate> <Intersoft:WrapPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </Intersoft:UXItemsControl.ItemsPanel> </Intersoft:UXItemsControl> |
Property | Value |
---|---|
Padding | 0 |
ToolTipService.ToolTip | {Binding Title} |
NavigateUri | {Binding NavigateUri} |
TargetName | Frame1 |
Frame1 will be the name of the UXFrame on the main page |
XAML |
Copy Code
|
---|---|
<Intersoft:UXItemsControl ...> ... <Intersoft:UXItemsControl.ItemTemplate> <DataTemplate> <Intersoft:UXHyperlinkButton Padding="0" ToolTipService.ToolTip="{Binding Title}" NavigateUri="{Binding NavigateUri}" TargetName="Frame1"> <Image Source="{Binding Image}" Width="76"/> </Intersoft:UXHyperlinkButton> </DataTemplate> </Intersoft:UXItemsControl.ItemTemplate> </Intersoft:UXItemsControl> |
This section shows how to create a page which details the selected Book, the page is using the BookDetail.xaml.
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
|
---|---|
xmlns:Model="clr-namespace:ItemDetailUXFrame.Models" |
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage.DataContext> <Model:Book /> </Intersoft:UXPage.DataContext> |
Property | Value |
---|---|
HeaderWidth | 70 |
Header | ID: |
Price: | |
Author: | |
Category: |
Sample Code |
Copy Code
|
---|---|
protected override void OnNavigatedTo(NavigationEventArgs e) { if (this.NavigationContextExtended.QueryString.Count > 0) { string id = this.NavigationContextExtended.QueryString["ID"]; if (!string.IsNullOrEmpty(id)) { BookListViewModel books = new BookListViewModel(); this.DataContext = books.FilteredBooks.SingleOrDefault(book => book.ID == id); } } } |
This section shows how to create the main page which hosts the UXFrame and a UXJournalButtons, the page is using BookContainer.xaml.
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 |
---|---|
Name | Frame1 |
HorizontalAlignment | Stretch |
VerticalAlignment | Stretch |
Margin | 10,24,10,10 |
UseGlobalShell | True |
Source | /BookList |
/BookList is a URI name set in the next step which refers to the BookList view. |
Sample Code |
Copy Code
|
---|---|
<Intersoft:UXFrame.UriMapper> <Intersoft:UriMapper> <Intersoft:UriMapping Uri="/BookList" MappedUri="/ItemDetailUXFrame;ItemDetailUXFrame.Views.BookList"/> <Intersoft:UriMapping Uri="/Books/{ID}" MappedUri="/ItemDetailUXFrame;ItemDetailUXFrame.Views.BookDetail?ID={ID}"/> </Intersoft:UriMapper> </Intersoft:UXFrame.UriMapper> |
Property | Value |
---|---|
VerticalAlignment | Top |
HorizontalAlignment | Left |
ForwardButtonVisibility | Collapsed |
Height | 24 |
Margin | 24 |
Height | 10,0,0,0 |
In order to view the result, you will need to build the Silverlight project and run the test page aspx on a browser.
In this walkthrough, you have learned how to create ClientUI MVVM Application project using Intersoft ClientUI MVVM Application project template, and create classes and page based on the MVVM pattern. You also learned how to utilize navigation feature using UXHyperlinkButton and UXJournalButtons in UXFrame.
For more information about application development using MVVM pattern, see MVVM Pattern Overview. For more information about navigation framework and its concepts, see Navigation Overview.
This section lists the complete code used in this walkthrough.
Source Code |
Copy Code
|
---|---|
using System.Collections.ObjectModel; using System.Linq; using ItemDetailUXFrame.Models; using System.Windows.Resources; using System; using System.Xml.Linq; namespace ItemDetailUXFrame.ViewModels { public class BookListViewModel : ViewModelBase { public ObservableCollection<Book> FilteredBooks { get; set; } public BookListViewModel() { this.LoadBooks(); } private void LoadBooks() { // loads book data from xml file StreamResourceInfo resource = System.Windows.Application.GetResourceStream( new Uri("Intersoft.ClientUI.Samples.Assets;component/Data/BookDataSource.xml", UriKind.Relative)); XDocument doc = XDocument.Load(resource.Stream); var filteredBooks = (from x in doc.Descendants("Book") where x.Element("Category").Value.Trim() == "Computing and Internet" select new Book(x)).Take(8); FilteredBooks = new ObservableCollection<Book>(filteredBooks); } } } |
Source Code |
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 ItemDetailUXFrame.ViewModels; using System.Xml.Linq; namespace ItemDetailUXFrame.Models { public class Book : ModelBase { public Book() { } public Book(XElement x) : this() { this._author = x.Element("Author").Value.Trim(); this._title = x.Element("Title").Value.Trim(); this._category = x.Element("Category").Value.Trim(); this._ID = x.Element("ID").Value.Trim(); this._price = double.Parse(x.Element("Price").Value.Trim()); this._image = new Uri("/Intersoft.ClientUI.Samples.Assets;component/Images/Books/" + x.Element("Image").Value.Trim(), UriKind.RelativeOrAbsolute); } private Uri _image = null; private string _author; private string _title; private string _category; private string _ID; private double _price; public string Author { get { return _author; } set { if (_author != value) { _author = value; OnPropertyChanged("Author"); } } } public string Title { get { return _title; } set { if (_title != value) { _title = value; OnPropertyChanged("Title"); } } } public string Category { get { return _category; } set { if (_category != value) { _category = value; OnPropertyChanged("Category"); } } } public string ID { get { return _ID; } set { if (_ID != value) { _ID = value; OnPropertyChanged("ID"); } } } public double Price { get { return _price; } set { if (_price != value) { _price = value; OnPropertyChanged("Price"); } } } public Uri Image { get { return _image; } set { if (_image != value) { _image = value; OnPropertyChanged("Image"); } } } public Uri NavigateUri { get { return new Uri("/Books/" + this.ID, UriKind.RelativeOrAbsolute); } } } } |
Sample Code |
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:ViewModels="clr-namespace:ItemDetailUXFrame.ViewModels" mc:Ignorable="d" xmlns:Intersoft="http://intersoft.clientui.com/schemas" x:Class="ItemDetailUXFrame.Views.BookList" Title="BookList Page" d:DesignWidth="640" d:DesignHeight="480"> <Intersoft:UXPage.DataContext> <ViewModels:BookListViewModel/> </Intersoft:UXPage.DataContext> <Grid x:Name="LayoutRoot"> <Intersoft:UXItemsControl HorizontalAlignment="Stretch" Name="uXItemsControl1" VerticalAlignment="Stretch" ItemsSource="{Binding Path=FilteredBooks}"> <Intersoft:UXItemsControl.ItemsPanel> <ItemsPanelTemplate> <Intersoft:WrapPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </Intersoft:UXItemsControl.ItemsPanel> <Intersoft:UXItemsControl.ItemTemplate> <DataTemplate> <Intersoft:UXHyperlinkButton Padding="0" ToolTipService.ToolTip="{Binding Title}" NavigateUri="{Binding NavigateUri}" TargetName="Frame1"> <Image Source="{Binding Image}" Width="76"/> </Intersoft:UXHyperlinkButton> </DataTemplate> </Intersoft:UXItemsControl.ItemTemplate> </Intersoft:UXItemsControl> </Grid> </Intersoft:UXPage> |
Sample Code |
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:Model="clr-namespace:ItemDetailUXFrame.Models" mc:Ignorable="d" xmlns:Intersoft="http://intersoft.clientui.com/schemas" x:Class="ItemDetailUXFrame.Views.BookDetail" Title="BookDetail Page" d:DesignWidth="640" d:DesignHeight="480"> <Intersoft:UXPage.DataContext> <Model:Book /> </Intersoft:UXPage.DataContext> <Grid x:Name="LayoutRoot"> <Intersoft:DockPanel HorizontalAlignment="Stretch" Name="dockPanel1" VerticalAlignment="Stretch" FillChildMode="Custom"> <TextBlock Name="textBlock1" Intersoft:DockPanel.Dock="Top" TextWrapping="Wrap" HorizontalAlignment="Left" Text="{Binding Path=Title}" /> <Image Name="image1" Stretch="None" Intersoft:DockPanel.Dock="Left" Width="220" Source="{Binding Path=Image}" VerticalAlignment="Top" /> <Intersoft:UXStackPanel Name="uXStackPanel1" Intersoft:DockPanel.Dock="Right" Intersoft:DockPanel.IsFillElement="True" Orientation="Vertical"> <Intersoft:FieldLabel Header="ID: " Name="fieldLabel1" HeaderWidth="70"> <TextBlock Name="textBlock2" Text="{Binding Path=ID}" VerticalAlignment="Center" /> </Intersoft:FieldLabel> <Intersoft:FieldLabel Header="Price: " Name="fieldLabel2" HeaderWidth="70"> <TextBlock Name="textBlock3" Text="{Binding Path=Price}" VerticalAlignment="Center" /> </Intersoft:FieldLabel> <Intersoft:FieldLabel Header="Author: " Name="fieldLabel3" HeaderWidth="70"> <TextBlock Name="textBlock4" VerticalAlignment="Center" Text="{Binding Path=Author}" /> </Intersoft:FieldLabel> <Intersoft:FieldLabel Header="Category: " Name="fieldLabel4" HeaderWidth="70"> <TextBlock Name="textBlock5" Text="{Binding Path=Category}" VerticalAlignment="Center" /> </Intersoft:FieldLabel> </Intersoft:UXStackPanel> </Intersoft:DockPanel> </Grid> </Intersoft:UXPage> |
Sample Code |
Copy Code
|
---|---|
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Intersoft.Client.UI.Navigation; using ItemDetailUXFrame.ViewModels; namespace ItemDetailUXFrame.Views { public partial class BookDetail : UXPage { public BookDetail() { InitializeComponent(); } // Executes when the user navigates to this page. protected override void OnNavigatedTo(NavigationEventArgs e) { if (this.NavigationContextExtended.QueryString.Count > 0) { string id = this.NavigationContextExtended.QueryString["ID"]; if (!string.IsNullOrEmpty(id)) { BookListViewModel books = new BookListViewModel(); this.DataContext = books.FilteredBooks.SingleOrDefault(book => book.ID == id); } } } } } |
Sample Code |
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" mc:Ignorable="d" x:Class="ItemDetailUXFrame.Views.BookContainer" Title="BookContainer Page" d:DesignWidth="640" d:DesignHeight="480"> <Grid x:Name="LayoutRoot"> <Intersoft:UXJournalButtons VerticalAlignment="Top" Height="24" HorizontalAlignment="Left" ForwardButtonVisibility="Collapsed" Margin="10,0,0,0" NavigationTarget="{Binding ElementName=Frame1}" /> <Intersoft:UXFrame x:Name="Frame1" HorizontalAlignment="Stretch" Margin="10,24,10,10" VerticalAlignment="Stretch" Source="/BookList" UseGlobalShell="True"> <Intersoft:UXFrame.UriMapper> <Intersoft:UriMapper> <Intersoft:UriMapping Uri="/BookList" MappedUri="/ItemDetailUXFrame;ItemDetailUXFrame.Views.BookList"/> <Intersoft:UriMapping Uri="/Books/{ID}" MappedUri="/ItemDetailUXFrame;ItemDetailUXFrame.Views.BookDetail?ID={ID}"/> </Intersoft:UriMapper> </Intersoft:UXFrame.UriMapper> </Intersoft:UXFrame> </Grid> </Intersoft:UXPage> |