WPF: Text-Box:DataBinding

 
Objective : Learning window Class
  • BindingOperations.ClearBinding
  • lBind.Path = new System.Windows.PropertyPath("Text");
    label1.SetBinding(ContentProperty, lBind);
References : Data Binding

http://msdn.microsoft.com/en-us/magazine/cc163299.aspx

 

Brief Overview of  Data Binding

  • Source , Target and Binding

Step: 1 Create a new project

Step: 2 Add one label control , text-box and two button contols

App.xml

Note StartupUri is set Mainwindows1.xaml; that triggers the page to load

 

Step: 3 Code MainWindow.xaml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace mmWpfWindowClass1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// String str1, str2;
this.Title = "Main Window in Code Only";
this.Width = 300;
this.Height = 300;
textBox1.Text = "Please Type here";

}

private void button1_Click(object sender, RoutedEventArgs e)
{
if (textBox1.Text != "")
{
BindingOperations.ClearBinding(this.label1, ContentProperty);
this.Width = 300;
this.Height = 300;
this.Background = Brushes.Azure;
textBox1.Text = "";


}

}

private void button2_Click(object sender, RoutedEventArgs e)
{

Binding lBind = new Binding();
lBind.ElementName = "textBox1";
lBind.Path = new System.Windows.PropertyPath("Text");
label1.SetBinding(ContentProperty, lBind);

}
}
}
 

Step: 3 Runtime views

Click On Invoke Binding, then clear binding