Ep

Ep

One Way Two Way WPF Data Binding Example C#

In the previous article I explained about Binding Mode in WPF like One Way Binding, Two Way Binding, INPC, Element Binding, Data Context, List Binding, Data Templates and Data Conversion. So in this blog post I will share an example to explain what is the difference between one way and two way binding in wpf.

Related Articles

One Way WPF DataBinding Example C#

The simplest form of Data Binding is One Way Binding, in which data is bound from its source (that is the object that holds the data) to its target (that is the object that displays the data). The following example will demonstrate you how to implement one way databinding in WPF, find the C# code below :
Wpf for Beginner databinding in wpf wpf data binding c# wpf data binding tutorial binding mode in wpf wpf binding path wpf datacontext
One Way Binding WPF
MainWindow.Xaml
<Window x:Class="DataBindingOneWay.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="One Way WPF Data Binding" Height="350" Width="525">
    <Grid>
        <StackPanel Name="Display">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name: " />
                <TextBlock   Margin="5,0,0,0"
                       Text="{Binding Name}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Designation: " />
                <TextBlock  Margin="5,0,0,0"
                       Text="{Binding Designation}" />
            </StackPanel>
        </StackPanel>
    </Grid>
</Window>

Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBindingOneWay
{
   public class Employee
   {
      public string Name { get; set; }
      public string Designation { get; set; }

      public static Employee GetEmployee()
      {
         var emp = new Employee()
         {
            Name = "Ravi",
            Designation = "Developer"
         };
         return emp;
      }

   }
}

MainWindow.xaml.cs
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
         DataContext = Employee.GetEmployee();
      }
   }

WPF Two Way Binding DataContext Example C#

In Two Way Binding, the user is able to modify the data through the user interface and have that data updated in the source. If the source changes while the user is looking at the view, you want the view to be updated. The following example will demonstrate you how to implement one way databinding in WPF, find the C# code below :-
wpf data binding example c#, wpf two way binding datacontext,two way binding in wpf datagrid,wpf two way binding not updating, wpf two way binding not working, wpf two way binding textbox
wpf two way binding textbox
MainWindow.xaml
<Window x:Class="DataBindingTwoWay.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WPF Two Way Binding Textbox" Height="350" Width="525">
    <Grid>
        <StackPanel Name="Display">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name: " />
                <TextBox Margin="5,0,0,0"
                       Text="{Binding Name, Mode=TwoWay}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Designation: " />
                <TextBox Margin="5,0,0,0"
                       Text="{Binding Designation, Mode=TwoWay}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name:" />
                <TextBlock Margin="5,0,0,0"
                       Text="{Binding Name}" />
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Designation:" />
                <TextBlock Margin="5,0,0,0"
                       Text="{Binding Designation}" />
            </StackPanel>

        </StackPanel>
    </Grid>
</Window>

Employee.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace DataBindingTwoWay
{
   public class Employee : INotifyPropertyChanged
   {
      private string name;
      public string Name
      {
         get { return name; }
         set
         {
            name = value;
            OnPropertyChanged();
         }
      }
      private string designation;
      public string Designation
      {
         get { return designation; }
         set
         {
            designation  = value;
            OnPropertyChanged();
         }
      }

      public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(
            [CallerMemberName] string caller = "" )
        {
            if ( PropertyChanged != null )
            {
                PropertyChanged( this,
                   new PropertyChangedEventArgs( caller ) );
            }
        }
   }
}
MainWindow.xaml.cs
namespace DataBindingTwoWay
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      private Employee emp;
      public MainWindow()
      {
         InitializeComponent();
         emp = new Employee()
         {
            Name = "Manish",
            Designation  = "QA"
         };
         DataContext = emp;
      }
   }
}

I hope you will enjoy the development tips while programming with C# in WPF. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. Also If you like this article, don't forget to share this article with your friends and colleagues
One Way Two Way WPF Data Binding Example C# Reviewed by Ravi Kumar on 1:23:00 AM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.