How To Create Object Data Source Using Microsoft Blend For Visual Studio

In this article, you will learn how to create object data source for UI elements, using Visual Studio and Microsoft Blend.

Before starting the tutorial, I assume that you are familiar with .NET 4.5 and Microsoft Blend for Visual Studio.

I have followed four principles along with MVVM pattern, in this article.

  • Simplicity
  • Blendability
  • Designability
  • Testability

Step 1

  • Create a new WPF Application project. Here, I have created a project named as "Test".


Step 2

  • Add a new folder to the project and rename it as "Model".
  • Add a new class in "Model" folder, and rename it as "Person".
  • Now, add the following code in "Person" class.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. namespace Test.Model {  
    7.     publicclass Person {  
    8.         private int _ID;  
    9.         publicint ID {  
    10.             get {  
    11.                 return _ID;  
    12.             }  
    13.             set {  
    14.                 _ID = value;  
    15.             }  
    16.         }  
    17.         private string _Name;  
    18.         publicstring Name {  
    19.             get {  
    20.                 return _Name;  
    21.             }  
    22.             set {  
    23.                 _Name = value;  
    24.             }  
    25.         }  
    26.         private string _Address;  
    27.         publicstring Address {  
    28.             get {  
    29.                 return _Address;  
    30.             }  
    31.             set {  
    32.                 _Address = value;  
    33.             }  
    34.         }  
    35.     }  
    36. }  
  • Again, add a new folder in the project, named as "Helper".
  • Add a new class, "ViewModelBase" in Helper folder.
  • Now, add the following code in ViewModelBase class.
    1. usingSystem;  
    2. usingSystem.Collections.Generic;  
    3. usingSystem.Linq;  
    4. usingSystem.Text;  
    5. usingSystem.Threading.Tasks;  
    6. usingSystem.ComponentModel;  
    7. usingSystem.Runtime.CompilerServices;  
    8. namespaceTest.Helper {  
    9.     public abstract class ViewModelBase: INotifyPropertyChanged {  
    10.         public eventPropertyChangedEventHandler PropertyChanged;  
    11.         protected voidIPropertyChanged(string PropertyName) {  
    12.             PropertyChangedEventHandlerHandler = this.PropertyChanged;  
    13.             if (Handler != null) PropertyChanged(this, newPropertyChangedEventArgs(PropertyName));  
    14.         }  
    15.         protected boolSetProperty < T > (ref T Storage, T Value, [CallerMemberName] stringPropertyName = null) {  
    16.             if (EqualityComparer < T > .Default.Equals(Storage, Value)) return false;  
    17.             Storage = Value;  
    18.             IPropertyChanged(PropertyName);  
    19.             return true;  
    20.         }  
    21.     }  
    22. }  

With the release of Microsoft Visual Studio 2012 and .NET 4.5, the company has introduced some lovely features; for example,  [CallerMemberName] makes its entrance.

With this new feature, you can write INotifyPropertyChanged (Interface) code without having to worry about renaming the properties.

  • Add a new folder, "Service", in the project.
  • Add new Interface named as "IPersonService" in Service folder.
  • Add the following code in IPersonService.
    1. using System;  
    2. usingSystem.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. usingSystem.Threading.Tasks;  
    6. namespace Test.Service {  
    7.     public interface IPersonService {  
    8.         IEnumerable < Model.Person > GetAllDetails();  
    9.     }  
    10. }  
  • Add a new class in Service folder, named "PersonServiceDump".
  • Add the following code in PersonServiceDump class.
    1. usingSystem;  
    2. usingSystem.Collections.Generic;  
    3. usingSystem.Linq;  
    4. usingSystem.Text;  
    5. usingSystem.Threading.Tasks;  
    6. namespaceTest.Service {  
    7.     public class PersonServiceDump: IPersonService {  
    8.         public IEnumerable < Model.Person > GetAllDetails() {  
    9.             var List = new List < Model.Person > ();  
    10.             List.Add(new Model.Person() {  
    11.                 ID = 101,  
    12.                     Name = "Karthik",  
    13.                     Address = "Pune",  
    14.                     Department = "Tech",  
    15.                     Team = "Dev"  
    16.             });  
    17.             return List;  
    18.         }  
    19.     }  
    20. }  
  • Add another folder, "ViewModel", in the Test project, along with a new class "MainWindowViewMode".
  • Add the following code in MainWindowViewModel.
    1. usingSystem;  
    2. usingSystem.Collections.Generic;  
    3. usingSystem.Linq;  
    4. usingSystem.Text;  
    5. usingSystem.Threading.Tasks;  
    6. usingSystem.Collections.ObjectModel;  
    7. namespaceTest.ViewModel {  
    8.     public class MainWindowViewModel: Helper.ViewModelBase {  
    9.         public MainWindowViewModel() {  
    10.             StartUp();  
    11.         }  
    12.         public void StartUp() {  
    13.             LoadAllDetails();  
    14.         }  
    15.         public void LoadAllDetails() {  
    16.             var Data = IService.GetAllDetails();  
    17.             MaserData = new ObservableCollection < Model.Person > (Data);  
    18.         }  
    19.         private ObservableCollection < Model.Person > _MaserData;  
    20.         public ObservableCollection < Model.Person > MaserData {  
    21.             get {  
    22.                 return _MaserData;  
    23.             }  
    24.             set {  
    25.                 SetProperty(ref _MaserData, value);  
    26.             }  
    27.         }  
    28.         private Service.IPersonService_IService;  
    29.         public Service.IPersonServiceIService {  
    30.             get {  
    31.                 if (_IService == null) {  
    32.                     _IService = new Service.PersonServiceDump();  
    33.                 }  
    34.                 return _IService;  
    35.             }  
    36.             set {  
    37.                 _IService = value;  
    38.             }  
    39.         }  
    40.     }  
    41. }  
  • Add a new folder named as "View", move the MainWindow.Xaml file in View folder, and change the namespace accordingly.
  • Save all the changes.

    Model

Step 3

  • Open the Test project in Microsoft Blend for Visual Studio.
  • Add new List Box in Grid.
  • I have the changed the Grid background and List Box background. 
  • We can change the brush from properties.

  • Model

    Model

Step 4

  • Click View, then choose Data Window, as shown below.

    Model

  • Press "Create data source" and choose "Create Object Data Source".

    Model

  • Choose MainWindowViewModel, then press OK.

    Model

  • Drag MainWindowViewModel into Grid.

    Model

    Model

  • Select MasterData, then drag it into List Box. Once it’s dragged, we can see the data in List Box.

    Model

    Model

    Model

  • Save all the changes.
  • Given below is the complete code of XAML.
    1. <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ViewModel="clr-namespace:Test.ViewModel" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Test.View.MainWindow" Title="MainWindow" Height="472.559" Width="768.939">  
    2.     <Window.Resources>  
    3.         <ViewModel:MainWindowViewModel x:Key="MainWindowViewModelDataSource" d:IsDataSource="True" />  
    4.         <DataTemplate x:Key="PersonTemplate">  
    5.             <StackPanel Orientation="Horizontal">  
    6.                 <TextBlock Text="{BindingAddress}" Margin="5,0,20,0" />  
    7.                 <TextBlock Text="{BindingID}" Margin="0,0,20,0" />  
    8.                 <TextBlock Text="{BindingDepartment}" Margin="0,0,20,0" />  
    9.                 <TextBlock Text="{BindingTeam}" Margin="0,0,20,0" />  
    10.                 <TextBlock Text="{BindingName}" /> </StackPanel>  
    11.         </DataTemplate>  
    12.     </Window.Resources>  
    13.     <Grid DataContext="{Binding Source={StaticResourceMainWindowViewModelDataSource}}">  
    14.         <Grid.Background>  
    15.             <LinearGradientBrush EndPoint="0.58,0.883" StartPoint="0.567,0">  
    16.                 <GradientStop Color="#FFA7B8B9" />  
    17.                 <GradientStop Color="#FF71E1EC" Offset="1" /> </LinearGradientBrush>  
    18.         </Grid.Background>  
    19.         <ListBox HorizontalAlignment="Left" Height="240" Margin="241.21,84.839,0,0" VerticalAlignment="Top" Width="227" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" ItemTemplate="{DynamicResource PersonTemplate}" ItemsSource="{BindingMaserData}" Foreground="White">  
    20.             <ListBox.Resources>  
    21.                 <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">  
    22.                     <VirtualizingStackPanelIsItemsHostVirtualizingStackPanelIsItemsHost="True">  
    23.                         <VirtualizingStackPanel.Background>  
    24.                             <LinearGradientBrushEndPointLinearGradientBrushEndPoint="0.5,1" StartPoint="0.5,0">  
    25.                                 <GradientStopColorGradientStopColor="Black" Offset="0" />  
    26.                                 <GradientStopColorGradientStopColor="#FF0CB2FF" Offset="1" /> </LinearGradientBrush>  
    27.                         </VirtualizingStackPanel.Background>  
    28.                         </VirtualizingStackPanel>  
    29.                 </ItemsPanelTemplate>  
    30.             </ListBox.Resources>  
    31.         </ListBox>  
    32.     </Grid>  
    33. </Window>  

Conclusion

I hope you liked this useful article. Please provide your valuable feedback and suggestions.


Similar Articles