Intersoft ClientUI 8 > ClientUI Fundamentals > Data Binding Overview > Data Binding Walkthroughs > Walkthrough: Bind to ViewModel from Style Setter in XAML using PropertyBinding |
This walkthrough shows how to bind properties through the style setter in the ItemContainerStyle using MVVM Pattern and ClientUI Property Binding.
In this walkthrough, you perform the following tasks:
You need the following components to complete this walkthrough:
This section shows how to create the Nation model class that represents the data entity used in this walkthrough.
C# |
Copy Code
|
---|---|
private string _association; public string Association { get { return this._association; } set { if (this._association != value) { this._association = value; this.OnPropertyChanged("Association"); } } } |
C# |
Copy Code
|
---|---|
private string _flag; private string _category; public string Flag { get { return this._flag; } set { if (this._flag != value) { this._flag = value; this.OnPropertyChanged("Flag"); } } } public string Name { get { return this._name; } set { if (this._name != value) { this._name = value; this.OnPropertyChanged("Name"); } } } |
C# |
Copy Code
|
---|---|
public static ObservableCollection<Nation> LoadData() { ObservableCollection<Nation> data = new ObservableCollection<Nation>(); data.Add(new Nation() { Association = "AFC", Name = "Australia", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Australia.png" }); data.Add(new Nation() { Association = "AFC", Name = "Japan", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Japan.png" }); data.Add(new Nation() { Association = "AFC", Name = "Korea DPR", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Korea DPR.png" }); data.Add(new Nation() { Association = "AFC", Name = "Korea Republic", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Korea Republic.png" }); data.Add(new Nation() { Association = "CAF", Name = "Algeria", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Algeria.png" }); data.Add(new Nation() { Association = "CAF", Name = "Cameroon", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Cameroon.png" }); data.Add(new Nation() { Association = "CAF", Name = "Côte d'Ivoire", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Côte d'Ivoire.png" }); data.Add(new Nation() { Association = "CAF", Name = "Ghana", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Ghana.png" }); data.Add(new Nation() { Association = "CAF", Name = "Nigeria", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Nigeria.png" }); data.Add(new Nation() { Association = "CAF", Name = "South Africa", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/South Africa.png" }); data.Add(new Nation() { Association = "CONCACAF", Name = "Honduras", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Honduras.png" }); data.Add(new Nation() { Association = "CONCACAF", Name = "Mexico", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Mexico.png" }); data.Add(new Nation() { Association = "CONCACAF", Name = "USA", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/USA.png" }); data.Add(new Nation() { Association = "CONMEBOL", Name = "Argentina", Flag = "ClientUIMVVMApp2;component/Assets/Flags/Argentina.png" }); data.Add(new Nation() { Association = "CONMEBOL", Name = "Brazil", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Brazil.png" }); data.Add(new Nation() { Association = "CONMEBOL", Name = "Chile", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Chile.png" }); data.Add(new Nation() { Association = "CONMEBOL", Name = "Paraguay", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Paraguay.png" }); data.Add(new Nation() { Association = "CONMEBOL", Name = "Uruguay", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Uruguay.png" }); data.Add(new Nation() { Association = "OFC", Name = "New Zealand", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/New Zealand.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Denmark", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Denmark.png" }); data.Add(new Nation() { Association = "UEFA", Name = "England", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/England.png" }); data.Add(new Nation() { Association = "UEFA", Name = "France", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/France.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Germany", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Germany.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Greece", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Greece.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Italy", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Italy.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Netherlands", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Netherlands.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Portugal", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Portugal.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Serbia", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Serbia.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Slovakia", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Slovakia.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Slovenia", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Slovenia.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Spain", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Spain.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Switzerland", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Switzerland.png" }); return data; } |
C# |
Copy Code
|
---|---|
public Nation()
{
} |
This section steps you through the process of creating a page that use UXComboBox. The UXComboBox is used to display a collection of Nation data.
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 |
---|---|
HorizontalAlignment | Center |
VerticalAlignment | Center |
Width | 200 |
DropDownHeight | 200 |
XAML |
Copy Code
|
---|---|
<Grid x:Name="LayoutRoot"> <Intersoft:UXComboBox HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" DropDownHeight="200"> </Intersoft:UXComboBox> </Grid> |
XAML |
Copy Code
|
---|---|
<Intersoft:UXComboBox HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" DropDownHeight="200"> <Intersoft:UXComboBox.ItemContainerStyle> <Style TargetType="Intersoft:UXComboBoxItem"> <Setter Property="ContentType" Value="ContentAndImage"/> </Style> </Intersoft:UXComboBox.ItemContainerStyle> </Intersoft:UXComboBox> |
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 Nations property to represent an observable collection of Nation model.
C# |
Copy Code
|
---|---|
... public class ShowNationViewModel : ViewModelBase { public ShowNationViewModel() { // sample data. this.Nations = Nation.LoadData(); } private ObservableCollection<Nation> _nations; public ObservableCollection<Nation> Nations { get { return this._nations; } set { if (this._nations != value) { this._nations = value; this.OnPropertyChanged("Nations"); } } } } ... |
This section show how to bind the ViewModel that was created in the previous section to the View.
In the previous sections, you have learned how to create the Model and ViewModel classes, as well as the View that contains the user interface and controls used in this walkthrough. This section shows how to instantiate the ViewModel in the XAML page and bind the UI elements to the properties in the ViewModel such as the data context.
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:ViewModels="clr-namespace:ClientUIMVVMApp2.ViewModels" mc:Ignorable="d" xmlns:Intersoft="http://intersoft.clientui.com/schemas" x:Class="ClientUIMVVMApp2.Views.ShowNation" Title="ShowNation Page" d:DesignWidth="640" d:DesignHeight="480"> .. <Intersoft:UXPage.Resources> <ViewModels:ShowNationViewModel x:Key="ShowNationViewModel"></ViewModels:ShowNationViewModel> </Intersoft:UXPage.Resources> ... |
XAML |
Copy Code
|
---|---|
... <Intersoft:UXComboBox ItemsSource="{Binding Source={StaticResource ShowNationViewModel}, Path=Nations}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" DropDownHeight="200"> <Intersoft:UXComboBox.ItemContainerStyle> <Style TargetType="Intersoft:UXComboBoxItem"> <Setter Property="ContentType" Value="ContentAndImage"/> </Style> </Intersoft:UXComboBox.ItemContainerStyle> </Intersoft:UXComboBox> ... |
XAML |
Copy Code
|
---|---|
... <Intersoft:UXComboBox ItemsSource="{Binding Source={StaticResource ShowNationViewModel}, Path=Nations}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" DropDownHeight="200"> <Intersoft:UXComboBox.ItemContainerStyle> <Style TargetType="Intersoft:UXComboBoxItem"> <Setter Property="ContentType" Value="ContentAndImage"/> <Setter Property="Intersoft:BindingFramework.PropertyBinding"> <Setter.Value> <Intersoft:PropertyBinding> <Intersoft:PropertyBinding Property="Icon"/> <Intersoft:PropertyBinding Property="Content"/> </Intersoft:PropertyBinding> </Setter.Value> </Setter> </Style> </Intersoft:UXComboBox.ItemContainerStyle> </Intersoft:UXComboBox> ... |
XAML |
Copy Code
|
---|---|
... <Intersoft:UXComboBox.ItemContainerStyle> <Style TargetType="Intersoft:UXComboBoxItem"> <Setter Property="ContentType" Value="ContentAndImage"/> <Setter Property="Intersoft:BindingFramework.PropertyBinding"> <Setter.Value> <Intersoft:PropertyBinding> <Intersoft:PropertyBinding Property="Icon" Binding="{Binding Flag}"/> <Intersoft:PropertyBinding Property="Content" Binding="{Binding Name}"/> </Intersoft:PropertyBinding> </Setter.Value> </Setter> </Style> </Intersoft:UXComboBox.ItemContainerStyle> ... |
C# |
Copy Code
|
---|---|
... private void Application_Startup(object sender, StartupEventArgs e) { this.RootVisual = new Views.ShowNation(); } ... |
For more information about ClientUI Binding Framework, see Data Binding 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 ClientUIMVVMApp2.ViewModels; using System.Collections.ObjectModel; namespace ClientUIMVVMApp2.Models { public class Nation : ModelBase { #region Constructors public Nation() { } #endregion #region Fields private string _association; private string _flag; private string _name; #endregion #region Properties public string Association { get { return this._association; } set { if (this._association != value) { this._association = value; this.OnPropertyChanged("Association"); } } } public string Flag { get { return this._flag; } set { if (this._flag != value) { this._flag = value; this.OnPropertyChanged("Flag"); } } } public string Name { get { return this._name; } set { if (this._name != value) { this._name = value; this.OnPropertyChanged("Name"); } } } public override string ToString() { return this.Name; } #endregion #region Methods public static ObservableCollection<Nation> LoadData() { ObservableCollection<Nation> data = new ObservableCollection<Nation>(); data.Add(new Nation() { Association = "AFC", Name = "Australia", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Australia.png" }); data.Add(new Nation() { Association = "AFC", Name = "Japan", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Japan.png" }); data.Add(new Nation() { Association = "AFC", Name = "Korea DPR", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Korea DPR.png" }); data.Add(new Nation() { Association = "AFC", Name = "Korea Republic", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Korea Republic.png" }); data.Add(new Nation() { Association = "CAF", Name = "Algeria", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Algeria.png" }); data.Add(new Nation() { Association = "CAF", Name = "Cameroon", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Cameroon.png" }); data.Add(new Nation() { Association = "CAF", Name = "Côte d'Ivoire", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Côte d'Ivoire.png" }); data.Add(new Nation() { Association = "CAF", Name = "Ghana", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Ghana.png" }); data.Add(new Nation() { Association = "CAF", Name = "Nigeria", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Nigeria.png" }); data.Add(new Nation() { Association = "CAF", Name = "South Africa", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/South Africa.png" }); data.Add(new Nation() { Association = "CONCACAF", Name = "Honduras", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Honduras.png" }); data.Add(new Nation() { Association = "CONCACAF", Name = "Mexico", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Mexico.png" }); data.Add(new Nation() { Association = "CONCACAF", Name = "USA", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/USA.png" }); data.Add(new Nation() { Association = "CONMEBOL", Name = "Argentina", Flag = "ClientUIMVVMApp2;component/Assets/Flags/Argentina.png" }); data.Add(new Nation() { Association = "CONMEBOL", Name = "Brazil", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Brazil.png" }); data.Add(new Nation() { Association = "CONMEBOL", Name = "Chile", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Chile.png" }); data.Add(new Nation() { Association = "CONMEBOL", Name = "Paraguay", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Paraguay.png" }); data.Add(new Nation() { Association = "CONMEBOL", Name = "Uruguay", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Uruguay.png" }); data.Add(new Nation() { Association = "OFC", Name = "New Zealand", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/New Zealand.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Denmark", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Denmark.png" }); data.Add(new Nation() { Association = "UEFA", Name = "England", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/England.png" }); data.Add(new Nation() { Association = "UEFA", Name = "France", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/France.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Germany", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Germany.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Greece", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Greece.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Italy", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Italy.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Netherlands", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Netherlands.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Portugal", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Portugal.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Serbia", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Serbia.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Slovakia", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Slovakia.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Slovenia", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Slovenia.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Spain", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Spain.png" }); data.Add(new Nation() { Association = "UEFA", Name = "Switzerland", Flag = "/ClientUIMVVMApp2;component/Assets/Flags/Switzerland.png" }); return data; } #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:ViewModels="clr-namespace:ClientUIMVVMApp2.ViewModels" mc:Ignorable="d" xmlns:Intersoft="http://intersoft.clientui.com/schemas" x:Class="ClientUIMVVMApp2.Views.ShowNation" Title="ShowNation Page" d:DesignWidth="640" d:DesignHeight="480"> <Intersoft:UXPage.Resources> <ViewModels:ShowNationViewModel x:Key="ShowNationViewModel"></ViewModels:ShowNationViewModel> </Intersoft:UXPage.Resources> <Grid x:Name="LayoutRoot"> <Intersoft:UXComboBox ItemsSource="{Binding Source={StaticResource ShowNationViewModel}, Path=Nations}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" DropDownHeight="200"> <Intersoft:UXComboBox.ItemContainerStyle> <Style TargetType="Intersoft:UXComboBoxItem"> <Setter Property="ContentType" Value="ContentAndImage"/> <Setter Property="Intersoft:BindingFramework.PropertyBinding"> <Setter.Value> <Intersoft:PropertyBinding> <Intersoft:PropertyBinding Property="Icon" Binding="{Binding Flag}"/> <Intersoft:PropertyBinding Property="Content" Binding="{Binding Name}"/> </Intersoft:PropertyBinding> </Setter.Value> </Setter> </Style> </Intersoft:UXComboBox.ItemContainerStyle> </Intersoft:UXComboBox> </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 ClientUIMVVMApp2.Models; using System.Collections.ObjectModel; namespace ClientUIMVVMApp2.ViewModels { public class ShowNationViewModel : ViewModelBase { public ShowNationViewModel() { // sample data. this.Nations = Nation.LoadData(); } private ObservableCollection<Nation> _nations; public ObservableCollection<Nation> Nations { get { return this._nations; } set { if (this._nations != value) { this._nations = value; this.OnPropertyChanged("Nations"); } } } } } |