Monthly Archives: February 2010

Binding Object Properties – Mode

The binding mode sets in which direction data will flow. Data usually flows from the source to the destination. There are five data binding modes.

Mode Description Direction of Flow
OneWayToSource From destination to source .
OneWay From source to destination.
OneTime The property is initially set, but updates to the source are not copied to the destination.
TwoWay Changes to source and destination are copied to each other.
Default Depends on the UI control.  

The default Mode varies between one-way and two-way depending on the dependency property that is being bound. You can always declare the binding mode explicitly to ensure that your binding has the desired behavior. In general, user-editable control properties, such as TextBox.Text and RangeBase.Value, default to two-way bindings, whereas most other properties default to one-way bindings.

The following code explains the concepts in details.

<Window x:Class="Bindingtoclasses.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
        Title="MainWindow" Height="350" Width="525">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Name="TextBox1" Grid.Row="0" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=TwoWay}"/>
        <TextBox Name="TextBox2" Grid.Row="1" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=OneWayToSource}"></TextBox>
        <TextBox Name="TextBox3" Grid.Row="2" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
        <TextBox Name="TextBox4" Grid.Row="3" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=OneWay}"></TextBox>
        <TextBlock Name="TextBlock1" Grid.Row="4" Text="{Binding Path=EmployeeNameTest.EmployeeName, Mode=OneTime}"/>
        <TextBlock Name="TextBlock2" Grid.Row="5" Text="{Binding Path=AnotherField, Mode=TwoWay}"/>
        
    </Grid>
</Window>

  • TextBox1 binds TwoWay, so it updates the source whenever the content changes as well as it gets updated when the source is changed (for ex, from TextBox2. It updates the source when the control loses focus.
  • TextBox2 binds OneWayToSource, so it gets populated once and this textbox never gets updated when the source changes. When the user types in the textbox, it updates the source with the value that it holds.
  • TextBox3 binds TwoWay, so it updates the source whenever the content changes as well as it gets updated when the source is changed . The difference between TextBox1 & TextBox3 is that, TextBox1 gets updates the source when the control loses focus and TextBox3 updates the source as the user types in because of UpdateSourceTrigger.
  • TextBox3 binds OneWay so it never updates the source. The data moves from source to target. So when the source is changed in any way, this control gets updated.

Download the source code from here.

"The older I get the less I listen to what people say and the more I look at what they do."— Andrew Carnegie

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