3.1_DataBinding.pptx
3.1_DataBinding.pptx
Desktop Application
Learning Outcomes
01 02 03
Create the UI for Style controls Basic data binding
ExpenseReportPage concepts
04 05 06
Binding DataGrid Connect SQL Server Database
to WPF Application
01
Create the UI for
ExpenseReportPage
3. Create the UI for ExpenseReportPage
➢ ExpenseReportPage.xaml displays the
expense report for the person that's
selected on the ExpenseItHome page.
➢ Let’s create the UI for ExpenseReportPage.
Open ExpenseReportPage.xaml to
add XAML lines between the Grid
tags.
ExpenseReportPage.xaml
Make watermark.png a
The opening grid’s background using
object of Grid ImageBrush
element.
Rows
ExpenseReportPage.xaml
TextBox
Employee
Employee.Name
Text
Notes:
➢ The target property must be a dependency property. Most UIElement properties are
dependency properties, and most dependency properties, except read-only ones, support
data binding by default. (Only types derived from DependencyObject can define
dependency properties; and all UIElement types derive from DependencyObject.)
➢ A Dependency Property is a property whose value depends on the external sources, such
as animation, data binding, styles, or visual tree inheritance. Not only this, but a
Dependency Property also has the built-in feature of providing notification when the
property has changed, data binding and styling.
➢ It should be noted that the binding source object is not restricted to being a custom .NET
object. WPF data binding supports data in the form of .NET objects and XML. To provide
some examples, your binding source may be a UIElement, any list object, an ADO.NET or
Web Services object, or an XmlNode that contains your XML data.
04
Binding
2. Binding
Grid layout control
We want to create a form and connect data to controls
TextBox
Label
controls
controls
Let’s see how to implement the previous form:
Define
properties in
class to initialize
data into
MainWindow
class
Now let’s make a source of data to use binding:
➢ Now we can bind the data in XAML file using Binding property.
Bind data to controls:
Run the project
➢ OneWay binding causes changes to the source property to automatically update the
target property, but changes to the target property are not propagated back to the source
property.
➢ TwoWay binding causes changes to either the source property or the target property to
automatically update the other.
➢ OneWayToSource is the reverse of OneWay binding; it updates the source property when
the target property changes. One example scenario is if you only need to reevaluate the
source value from the UI.
TwoWay Binding Mode:
Add Button
control in last
Grid row
The event handler:
1 2
Change First
The message box Name to “Ahmed”
appears with the 3 then press Show
new TextBox data. button.
If we use OneWay
binding mode the
data will not
change.
Let’s complete our Mission !!!
Login page
User Name:
User name:
Age:
phone:
Address:
Edit Back
05
DataGrid
5. DataGrid
➢ The DataGrid control enables you to display and edit data from many different sources,
such as from a SQL database, LINQ query, or any other bindable data source.
➢ The ItemSource property of DataGrid is the key to data binding. You can bind any data
source that implements IEnuemerable. Each row in the DataGrid is bound to an object in
the data source and each column in the DataGrid is bound to a property of the data
source objects.
➢ Let’s take an example which creates a collection of objects and bind it to a DataGrid
control.
● LINQ query: Language-Integrated Query (LINQ) is the name for a set of technologies
based on the integration of query capabilities directly into the C# language. Traditionally,
queries against data are expressed as simple strings.
● IEnuemerable: It’s an interface that helps in iteration of a list collection or any kind of
collection.
DataGrid example:
Click on Tools
menu
Select Connect
to Database
Add your local server
name.
Select a database
You can get it from SQL
that you want to
Server Management
connect with.
Studio
Here we will choose
expense database.
A message box will
appear if the
connection is
succeeded or not
After choosing a
database click on Test
Connection button to
check if database is
connected
Click OK if you
ensure the database
is connected
Click on Tools
menu
Then click on
Connect
We can select the
database from
To execute this list box
the query
Write SQL
query here
● Now we can execute any
SQL query in VS.
● That helps us to see all
database changes.
The
expense_report
table
➢ In ExpenseIt page, when we
select a name and click on View
button, then should appears to us
the ExpenseReport page that
shows the expense data of the
selected name.
➢ To show the expense data of the
name that was selected in
ExpenseIt page, we should pass
that name to ExpenseReport
page at first.
➢ Let’s see how to do this…
Open
ExpenseItHome.xaml.cs
In Button_Click event
handler of View button
The output is a
just a name as
we wanted
Open
Bind data from database to controls ExpenseReportPage.xaml
● Create ShowTheReport()
function and call it in
constructor.
● This function should has
database connection and
display the data that we want
from database in controls
In Data Source Open
Bind data from database to controls ExpenseReportPage.xaml.cs
add the local
server name
Integrated Security
will be true if you
In Database(or Initial don’t add a password
Catalog) add the for database.
database name
CommandText
property assigned
with the query cmd object of
string. SqlCommand class
to pass our
command,
conn.Open() to
open the SQL
connection and
write the query. In the query we want to select all
report when Name = @empName
The variable empName assigned in
the next line with name (we get this
name from ExpenseIt page) by
cmd.Parameters.AddWithValue()
If we want to use variable in SQL query method.
then it should start with ‘@’ sign
Bind data from database to controls Open
ExpenseReportPage.xaml.cs
SqlDataAdapter
represents a set of data
commands and a
database connection that
are used to fill the
Create a DataSet and update a
DataTable and fill SQL Server database.
it by the
SqlDataAdapter
object Now bind with control by
setting the ItemsSource
property of a DataGrid to
the table of data.
ExpenseReportPage.xaml.cs
Output…
IMAGE
Output…
IMAGE
THANK YOU!