Sie sind auf Seite 1von 5

4/26/2019 Simple Data Binding in WPF

In Focus
 
First C# Corner

Simple Data Binding


ASK A QUESTION in WPF CONTRIBUTE

Here we look an example of simple data binding in WPF.

Mahak Gupta Apr 10 2012

0 7 276.8k

DataBindingInListControlInWPF.rar
Download Free .NET & JAVA Files API

Here we look an example of simple data binding in WPF. In this example I use a class for Data Binding.
Here we look at the program.

Step1: First we create a Grid in our project:

<Grid x:Name="StuInfo">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="77"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
</Grid>

Step2: After that we create two TextBlocks and Two TextBoxes in our program and we also create a Button
(Next) to see the Next Record according to the program.

<TextBlock Text="First Name" Margin="10"></TextBlock>


<TextBlock Text="Last Name" Margin="10" Grid.Row="1"></TextBlock>
<TextBox Text="{Binding fname}" Margin="10" Grid.Column="1"></TextBox>
<TextBox Text="{Binding lname}" Margin="10" Grid.Column="1" Grid.Row="1"></TextBox>
<Button HorizontalAlignment="Left" Margin="0,12,0,9" Name="button1" Width="75" Grid.Column="1"
Grid.Row="2">Next</Button>

https://www.c-sharpcorner.com/UploadFile/mahakgupta/simple-data-binding-in-wpf/ 1/5
4/26/2019 Simple Data Binding in WPF

In Focus
 
First C# Corner

ASK A QUESTION CONTRIBUTE

Step3: Now we add a Loaded Event handler in the .cs page. This event will be called when the client wants
to load the application:

public partial class Window1 : Window


{
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{

}
}

Step4: Now we add a class to our program:

public class Stu


{
public string fname { get; set; }
public string lname { get; set; }
}

Step5: Now we write the following code in the Loded Event Handler:

void Page_Loaded(object sender, RoutedEventArgs e)


{
Stu s = new Stu();
{
s.fname = "Mahak";
s.lname = "Garg";
};

https://www.c-sharpcorner.com/UploadFile/mahakgupta/simple-data-binding-in-wpf/ 2/5
4/26/2019 Simple Data Binding in WPF

In Focus
this.StuInfo.DataContext = s;
 
First
} C# Corner

As we mention Binding in TextBox in .xaml page : ASK A QUESTION CONTRIBUTE

<TextBox Text="{Binding fname}" Margin="10" Grid.Column="1"></TextBox>


<TextBox Text="{Binding lname}" Margin="10" Grid.Column="1" Grid.Row="1"></TextBox>

The output will be:

Step6: Now we add another Event Handler, which is for the Next Button:

public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
this.button1.Click += new RoutedEventHandler(button1_Click);
}
void button1_Click(object sender, RoutedEventArgs e)
{
Stu s = new Stu();
{
s.fname = "Juhi";
s.lname = "Gupta";
};
this.StuInfo.DataContext = s;
}

Here we add another data in our program. When we click on the Next Button, the output will be:

https://www.c-sharpcorner.com/UploadFile/mahakgupta/simple-data-binding-in-wpf/ 3/5
4/26/2019 Simple Data Binding in WPF

In Focus
 
First C# Corner
ASK A QUESTION CONTRIBUTE

Binding Data Binding in WPF WPF WPF tutorials

Mahak Gupta

My name is Mahak Gupta. I am a C# Corner MVP.


https://www.c-sharpcorner.com/members/mahak-gupta

102 4.4m 2

0 7

Type your comment here and press Enter Key (Minimum 10 characters)

cool
Asfend Yar Feb 03, 2016
441 3.9k 297.3k 0 0 Reply

its simple and very e ective.. thanks a lot


wikum wijeysooriya Aug 18, 2015
1686 94 0 0 0 Reply

EDIT: nevermind
K May 07, 2013
1779 1 0 0 0 Reply

@ Arjun The answer depends on the context actually.if its which objects we can bind to then it can
be any FrameWork Element but the property must be a Dependency property.and furthermore

https://www.c-sharpcorner.com/UploadFile/mahakgupta/simple-data-binding-in-wpf/ 4/5
4/26/2019 Simple Data Binding in WPF

In Focus the other context how we want to bind the data (I mean at compiletime or at runtime )then there
 
First C# Corner
is Static and Dynamic binding.actually binding in WPF is a huge topic but a very important one. I
hope this may be small help to you.
ASK A QUESTION CONTRIBUTE
tarun tripathi May 10, 2012
1765 15 0 0 0 Reply

It's a very helpful for beginners keep it up. I have a question to you that what's the need of WPF
when we have GDI, GDI+ and DirectX?
Shrutz Mhatz Apr 11, 2012
1753 27 17.8k 0 0 Reply

How many ways binding data in WPF?


Arjun Panwar Apr 10, 2012
184 10.5k 3.6m 0 0 Reply

We want to know how can be fetch data row by row from table in WPF?
Abhishek Dubey Apr 10, 2012
247 7.3k 1.8m 0 0 Reply

TRENDING UP

01 Most Popular Front End JavaScript Framework In The World

02 Top 10 JavaScript Frameworks In The World

03 Angular 7 Routing And Preserving Trailing Slash In URL⚔

04 .NET Core For .NET Developers

05 For Vs Foreach In C#

06 How To Implement Authentication Using Identity Model In ASP.NET Core

07 Create Reusable View Using Razor Class Library In ASP.NET Core

08 Easy SharePoint ListItem CRUD Operation Using REST API Wrapper

09 Facade Design Pattern Using C#

10 Visual Studio 2019 Xamarin New Features


View All

About Us Contact Us Privacy Policy Terms Media Kit Sitemap Report a Bug FAQ Partners
C# Tutorials
©2019 C# Corner. All contents are copyright of their authors.

https://www.c-sharpcorner.com/UploadFile/mahakgupta/simple-data-binding-in-wpf/ 5/5

Das könnte Ihnen auch gefallen