Posted by: Zeeshan Amjad | May 16, 2010

Clear the binding at run time


We saw several examples of binding, but sometimes we need to clear the binding at run time. There is one class BindingOperation to do the same thing. This class is directly inherited by Object class. Here is a class diagram of this class.

BindingOperations

Here is a simple program which demonstrate to set and remove the binding at run time. Here is complete XAML code of the program.

  1: <Window x:Class="WpfClearBinding.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     Title="Binding Example" Height="300" Width="300">
  5:     <Grid>
  6:         <Grid.RowDefinitions>
  7:             <RowDefinition Height="2*"/>
  8:             <RowDefinition Height="2*"/>
  9:             <RowDefinition/>
 10:         </Grid.RowDefinitions>
 11:         <Grid.ColumnDefinitions>
 12:             <ColumnDefinition/>
 13:             <ColumnDefinition/>
 14:         </Grid.ColumnDefinitions>
 15:         <Slider Name="slider" VerticalAlignment="Center" Minimum="0" Maximum="100"
 16:                 Grid.Row="0" Grid.ColumnSpan="2" Margin="5"/>
 17:         <TextBlock Name="text" Grid.Row="1" Grid.ColumnSpan="2" Margin="5"/>
 18:         <Button Name="setBinding" Grid.Row="2" Grid.Column="0" Margin="5" Click="setBinding_Click">
 19:             Set Binding
 20:         </Button>
 21:         <Button Name="clearBinding" Grid.Row="2" Grid.Column="1" Margin="5" Click="clearBinding_Click">
 22:             Clear Binding
 23:         </Button>
 24:     </Grid>
 25: </Window>
 26: 

Now we are going to write a handler of both buttons. Here is a code to do the binding using C# code.

  1: Binding binding = new Binding();
  2: binding.Source = slider;
  3: binding.Path = new PropertyPath("Value");
  4: text.SetBinding(TextBlock.TextProperty, binding);
  5: 

And here is C# code to clear the binding.

  1: BindingOperations.ClearBinding(text, TextBlock.TextProperty);

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 WpfClearBinding
 16: {
 17:     /// <summary>
 18:     /// Interaction logic for Window1.xaml
 19:     /// </summary>
 20:     public partial class Window1 : Window
 21:     {
 22:         public Window1()
 23:         {
 24:             InitializeComponent();
 25:         }
 26: 
 27:         private void setBinding_Click(object sender, RoutedEventArgs e)
 28:         {
 29:             Binding binding = new Binding();
 30:             binding.Source = slider;
 31:             binding.Path = new PropertyPath("Value");
 32:             text.SetBinding(TextBlock.TextProperty, binding);
 33:         }
 34: 
 35:         private void clearBinding_Click(object sender, RoutedEventArgs e)
 36:         {
 37:             BindingOperations.ClearBinding(text, TextBlock.TextProperty);
 38:         }
 39:     }
 40: }
 41: 

Here is the output of the program if we press the set binding button and move slider little bit.

ClearBindingOutput_01

And here is the output of the program when press the clear binding button.

ClearBindingOutput_02

Advertisements

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: