Intersoft ClientUI 8 > ClientUI Fundamentals > MVVM Pattern Overview > MVVM Pattern Walkthroughs > Walkthrough: Use UXSearchBox to Show Search Results with MVVM Pattern |
This walkthrough shows you how to create a UXSearchBox to search item in collection of data. 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 SampleData.cs. The model will map information in the data entity to a property.
C# |
Copy Code
|
---|---|
private string _displayName; public string DisplayName { get { return this._displayName; } set { if (this._displayName != value) { string description = value; description = description.Replace("||", "\r\n"); this._displayName = description; this.OnPropertyChanged("DisplayName"); } } } |
C# |
Copy Code
|
---|---|
public SampleData(XElement i) { this.Name = i.Attribute("Name").Value.ToString(); string displayName = i.Attribute("DisplayName").Value.ToString(); this.DisplayName = (string.IsNullOrEmpty(displayName) ? this.Name : displayName); this.Uri = i.Attribute("Uri").Value.ToString(); } |
This section shows how to create a ViewModel to hold the collection of SampleData and the selected SampleData model object . In order to implement search, the UXSearchBox requires IsSearching, QueryText and SearchResult property.
C# |
Copy Code
|
---|---|
public ObservableCollection<SampleData> Samples { get; set; } private ObservableCollection<SampleData> _searchResult; public ObservableCollection<SampleData> SearchResult { get { return this._searchResult; } set { if (this._searchResult != value) { this._searchResult = value; this.OnPropertyChanged("SearchResult"); } } } |
C# |
Copy Code
|
---|---|
private string _queryText; public string QueryText { get { return this._queryText; } set { if (this._queryText != value) { this._queryText = value; this.OnPropertyChanged("QueryText"); } } } |
C# |
Copy Code
|
---|---|
public SearchViewModel() { this.LoadSamples(); } private void LoadSamples() { StreamResourceInfo resource = System.Windows.Application.GetResourceStream(new Uri("/ClientUIMVVMSearchBox;component/Samples.xml", UriKind.Relative)); XDocument doc = XDocument.Load(resource.Stream); var samples = from d in doc.Descendants("SampleItem") select new SampleData(d); foreach (SampleData sample in samples) { this.Samples.Add(sample); } resource.Stream.Close(); } |
C# |
Copy Code
|
---|---|
private bool _isSearching; public bool IsSearching { get { return this._isSearching; } set { if (this._isSearching != value) { this._isSearching = value; this.OnPropertyChanged("IsSearching"); this.DoSearch(); } } } |
C# |
Copy Code
|
---|---|
private void DoSearch() { var query = from q in this.Samples where q.DisplayName.ToLower(CultureInfo.InvariantCulture).Contains(this.QueryText.ToLower(CultureInfo.InvariantCulture)) select q; ObservableCollection<SampleData> data = new ObservableCollection<SampleData>(); foreach (SampleData d in query) { data.Add(d); } this.SearchResult = data; this.IsSearching = false; } |
C# |
Copy Code
|
---|---|
private SampleData _selectedSampleData; public SampleData SelectedData { get { return _selectedSampleData; } set { if (_selectedSampleData != value) { _selectedSampleData = value; this.OnPropertyChanged("SelectedData"); MessageBoxServiceProvider.Show("Navigate to uri: " + _selectedSampleData.Uri, null); } } } |
This section describes how to create the view with the UXSearchBox.
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:UXPage ... xmlns:ViewModels="clr-namespace:ClientUIMVVMSearchBox.ViewModels" ... > ... </Intersoft:UXPage> |
XAML |
Copy Code
|
---|---|
<Intersoft:UXPage.DataContext> <ViewModels:SearchViewModel/> </Intersoft:UXPage.DataContext> |
Property | Value |
---|---|
HorizontalAlignment | Left |
VerticalAlignment | Top |
Margin | 12,12,0,0 |
Width | 300 |
DisplayMemberPath | DisplayName |
AutoShowResultBox | True |
WatermarkTextVisibility | Visible |
WatermarkText | Enter keyword |
WatermarkForeground | Black |
XAML |
Copy Code
|
---|---|
<Intersoft:UXSearchBox HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="300" DisplayMemberPath="DisplayName" WatermarkTextVisibility="Visible" WatermarkText="Enter keyword" AutoShowResultBox="True" WatermarkForeground="Black" IsSearching="{Binding IsSearching, Mode=TwoWay}" QueryText="{Binding QueryText, Mode=TwoWay}" SearchResult="{Binding SearchResult}" SelectedItem="{Binding SelectedData, Mode=TwoWay}"/> |
In order to view the result, you will need to build the Silverlight project and run the test page in the 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 search feature in UXSearchBox.
For more information about application development using MVVM pattern, see MVVM Pattern Overview. For more information about UXSearchBox control, see UXSearchBox Overview.
This section lists the complete code used in this walkthrough.
C# |
Copy Code
|
---|---|
using System.Collections.ObjectModel; using System.Linq; using System.Xml.Linq; using ClientUIMVVMSearchBox.ViewModels; namespace ClientUIMVVMSearchBox.Models { public class SampleData : ModelBase { #region Constructors public SampleData() { } public SampleData(XElement i) { this.Name = i.Attribute("Name").Value.ToString(); string displayName = i.Attribute("DisplayName").Value.ToString(); this.DisplayName = (string.IsNullOrEmpty(displayName) ? this.Name : displayName); this.Uri = i.Attribute("Uri").Value.ToString(); } #endregion #region Fields private string _displayName; private string _name; private string _uri; #endregion #region Properties public string DisplayName { get { return this._displayName; } set { if (this._displayName != value) { string description = value; description = description.Replace("||", "\r\n"); this._displayName = description; this.OnPropertyChanged("DisplayName"); } } } public string Name { get { return this._name; } set { if (this._name != value) { this._name = value; this.OnPropertyChanged("Name"); } } } public string Uri { get { return this._uri; } set { if (this._uri != value) { this._uri = value; this.OnPropertyChanged("Uri"); } } } #endregion } } |
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.Collections.ObjectModel; using ClientUIMVVMSearchBox.Models; using System.Globalization; using System.Linq; using System.Windows.Resources; using System.Xml.Linq; namespace ClientUIMVVMSearchBox.ViewModels { public class SearchViewModel : ViewModelBase { public SearchViewModel() { this.LoadSamples(); } private bool _isSearching; private string _queryText; private ObservableCollection<SampleData> _samples; private ObservableCollection<SampleData> _searchResult; private SampleData _selectedSampleData; public bool IsSearching { get { return this._isSearching; } set { if (this._isSearching != value) { this._isSearching = value; this.OnPropertyChanged("IsSearching"); this.DoSearch(); } } } public string QueryText { get { return this._queryText; } set { if (this._queryText != value) { this._queryText = value; this.OnPropertyChanged("QueryText"); } } } public ObservableCollection<SampleData> Samples { get { if (this._samples == null) this._samples = new ObservableCollection<SampleData>(); return this._samples; } } public ObservableCollection<SampleData> SearchResult { get { return this._searchResult; } set { if (this._searchResult != value) { this._searchResult = value; this.OnPropertyChanged("SearchResult"); } } } private void DoSearch() { var query = from q in this.Samples where q.DisplayName.ToLower(CultureInfo.InvariantCulture).Contains(this.QueryText.ToLower(CultureInfo.InvariantCulture)) select q; ObservableCollection<SampleData> data = new ObservableCollection<SampleData>(); foreach (SampleData d in query) { data.Add(d); } this.SearchResult = data; this.IsSearching = false; } private void LoadSamples() { StreamResourceInfo resource = System.Windows.Application.GetResourceStream( new Uri("/ClientUIMVVMSearchBox;component/Samples.xml", UriKind.Relative)); XDocument doc = XDocument.Load(resource.Stream); var samples = from d in doc.Descendants("SampleItem") select new SampleData(d); foreach (SampleData sample in samples) { this.Samples.Add(sample); } resource.Stream.Close(); } public SampleData SelectedData { get { return _selectedSampleData; } set { if (_selectedSampleData != value) { _selectedSampleData = value; this.OnPropertyChanged("SelectedData"); MessageBoxServiceProvider.Show("Navigate to uri: " + _selectedSampleData.Uri, null); } } } } } |
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:ClientUIMVVMSearchBox.ViewModels" mc:Ignorable="d" x:Class="ClientUIMVVMSearchBox.Views.Search" Title="Search Page" d:DesignWidth="640" d:DesignHeight="480"> <Intersoft:UXPage.DataContext> <ViewModels:SearchViewModel /> </Intersoft:UXPage.DataContext> <Grid x:Name="LayoutRoot"> <Intersoft:UXSearchBox HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="300" DisplayMemberPath="DisplayName" WatermarkTextVisibility="Visible" WatermarkText="Enter keyword" AutoShowResultBox="True" WatermarkForeground="Black" IsSearching="{Binding IsSearching, Mode=TwoWay}" QueryText="{Binding QueryText, Mode=TwoWay}" SearchResult="{Binding SearchResult}" SelectedItem="{Binding SelectedData, Mode=TwoWay}"/> </Grid> </Intersoft:UXPage> |