One-Way Data Binding in WPF

Introduction

In one-way data binding we fetch data from a data source and display it in a user interface. That's it. So let's do it.

First of all we need to create the data source. Here I have created three properties and one static method and assigned respective values to the properties.

Add one class file to the project and provide it the name Customer.cs.

cs code


Now it's time to create the user interface (View) that will show the data from the data source.

Add the following code to MainWindow.xaml:

xmal code

Now we have created the data source and user interface but they are disconnected from each other.

I mean we need to connect our UI (View) to the data source for showing the data on the page.

Now, I will call the static method getCustomer() and assign the value to the DataContext object.

Add the following line of code to MainWindow.xaml.cs:

  1. DataContext = Customer.getCustomer();  
Output

output

Summary

Here we have created a UI (View) and data source. Then we have fetched the data from the data source and shown the data to the user interface (View). This is called one-way data binding. In my next article we will talk about two-way binding where we have a request and response model.

Up Next
    Ebook Download
    View all
    Learn
    View all