Posted by: Zeeshan Amjad | January 30, 2010

Self Binding


Sometimes we need to bind one property of one control with another property of the same control. In that case we will use the self binding. Now we are going to see the simple example of the usage of self binding. Here we are making on small program to create on list box to display the state name. At the same times we are displaying tooltip to display the same text whatever is display in the list box item. Here is a piece of code to perform the self binding.

  1: <TextBlock Text="{Binding}" 
  2:    ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Text}">
  3: 

Rest of the coding is very simple. Here is a complete XAML code of the program.

  1: <Window x:Class="WpfSelfBinding.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Self Binding" Height="300" Width="300">
  5:     <Grid>
  6:         <ListBox Name="list" Margin="5">
  7:             <ListBox.ItemTemplate>
  8:                 <DataTemplate>
  9:                     <TextBlock Text="{Binding}" 
 10:                                ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Text}">
 11:                     </TextBlock>
 12:                 </DataTemplate>
 13:             </ListBox.ItemTemplate>
 14:         </ListBox>
 15:     </Grid>
 16: </Window>
 17: 

And here is complete C# code of the program.

  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: namespace WpfSelfBinding
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         public Window1()
 23:         {
 24:             List<String> stateList = new List<String>();
 25: 
 26:             stateList.Add("California");
 27:             stateList.Add("Taxes");
 28:             stateList.Add("Maryland");
 29:             stateList.Add("Washington");
 30:             stateList.Add("Arizona");
 31:             stateList.Add("Virginia");
 32: 
 33:             InitializeComponent();
 34: 
 35:             list.ItemsSource = stateList;
 36:         }
 37:     }
 38: }
 39: 

Here is a output of this program.

SelfBindingOutput

Advertisements

Responses

  1. […] Built in Commands We just saw the example of build in commands. We already studied about self binding. Now we are going to use these two concepts together and see one more advantage of self binding. […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: