Category Archives: Instantiating an Object as a resource in XAML.

Instantiating an Object as a resource in XAML.

The following steps can be followed to instantiate the object as a resource in XAML.

  • Imagine we have a class Person as mentioned below.
    Code Snippet
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Windows;
    6. using System.Windows.Controls;
    7. using System.Windows.Data;
    8. using System.Windows.Documents;
    9. using System.Windows.Input;
    10. using System.Windows.Media;
    11. using System.Windows.Media.Imaging;
    12. using System.Windows.Navigation;
    13. using System.Windows.Shapes;
    14.  
    15. using System.ComponentModel;
    16.  
    17. namespace DataBinding_Source_Property
    18. {
    19.     /// <summary>
    20.     /// Interaction logic for MainWindow.xaml
    21.     /// </summary>
    22.     public partial class MainWindow : Window
    23.     {
    24.         public MainWindow()
    25.         {
    26.             InitializeComponent();
    27.         }
    28.     }
    29.  
    30.     public class Person
    31.     {
    32.         private string fullname;
    33.         public string FullName
    34.         {
    35.             get
    36.             {
    37.                 return fullname;
    38.             }
    39.             set
    40.             {
    41.                 fullname = value;
    42.             }
    43.         }
    44.  
    45.     }
    46. }

Steps to be followed are:

  • Include the namespace of the class as shown in line5. Notice that it is assigned to xmlns:kishore. Here we are using name resolution kishore with XML namespace. It can be xmlns:local or xmln:src or any other word followed by xmlns:. We have to use the same identifier(kishore or local or src) when we refer (or when want to use) the namespace.
  1. <Window x:Class="DataBinding_Source_Property.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4.         Title="MainWindow" Height="350" Width="525"
  5.         xmlns:kishore="clr-namespace:DataBinding_Source_Property">

 

  • To use the classes in the above namespace, we need to create an object of the class we are interested in as shown below. Ex, Person class
    <Window.Resources>
        <kishore:Person x:Key="PersonXAMLDataSource" FullName="Kishore1021"/>
    </Window.Resources>

 

  • Once the object of the Person class is instantiated, you can use it as a source during binding operation to bind to some UI element as shown below.
<TextBox Text="{Binding Source={StaticResource PersonXAMLDataSource}, Path=FullName}"></TextBox>
        

 

The complete of the application is as follows.

Code Snippet
  1. <Window x:Class="DataBinding_Source_Property.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4.         Title="MainWindow" Height="350" Width="525"
  5.         xmlns:kishore="clr-namespace:DataBinding_Source_Property">
  6.    <Window.Resources>
  7.        <kishore:Person x:Key="PersonXAMLDataSource" FullName="Kishore1021"/>
  8.    </Window.Resources>
  9.     
  10.     <Grid>
  11.         <Grid.RowDefinitions>
  12.             <RowDefinition Height="*"> </RowDefinition>
  13.             <RowDefinition Height="*"></RowDefinition>
  14.         </Grid.RowDefinitions>
  15.         
  16.         <TextBox Text="{Binding Source={StaticResource PersonXAMLDataSource}, Path=FullName}"></TextBox>
  17.         
  18.     </Grid>
  19. </Window>

Download SourceCode from SkyDrive

"If leadership serves only the leader, it will fail. Ego satisfaction, financial gain, and status can all be valuable tools for a leader, but if they become the only motivations, they will eventually destroy a leader. Only when service for a common good is the primary purpose are you truly leading." – Sheila Murray Bethel