. If you see the
it can be any public property, CLR object, Ado.Net DataSet,
XMLDataProvider, ObjectDataProvider etc.
is a TextBox, Text property of this textbox is bind to TextBlock Text Property. You can see the Code and output below.
Now add a new window to your project and drag three textboxes ,three labels for displaying above three properties.
XAML code looks like below.
Now in the Constructor of the Window write below code
As you may see that we have created an instance of class
MobileShop and assigned the data for properties.
After that we assigned the
object to
DataContext Property of the Window. Here
DataContext will hold the Data. This property exist for almost all controls that derived from
Dependency Object i.e Panel Controls like Grid, Canvas, StackPanel, Wrappanel, Content Controls like Label, Button etc. If we set our object as
DataContext to a higher level control then all child controls can inherit that object. Read More About
DataContext in WPF.
So now in our present example we set object as
DataContext for window parent control so all the child controls have default
data source, so we don't need to set
Source we just need to mention the
Path to which it binds. So in our example we only mentioned
Path as below.
The same example can also be done without writing single peace of line code in code behind file, by writing only XAML code as below
Here first we have to set the namespace reference to access class files in the same project. i.e
xmlns:local="clr-namespace:WpfApplication1"
Now we can access all contents in the project using local reference like below.
Binding in Code behind file:
We can also perform data binding in code file.
- Create the Binding Object
- Set the Binding Source
- Set the path
- Bind the object to Target using SetBinding Method.
Here is an example on
how to perform binding in code behind file.
Mode is the one property of the
binding element which determines the flow of data between binding source and target. Four
Binding Modes available.
- OneWay.
- OneTime.
- OneWayToSource.
- TwoWay.
OneWay: If
Mode is
OneWay means
Data flows from source to target whenever source is changed.
 |
OneWay Binding Mode |
Here
Target UI will be updated only if the source changed. Use this to display read only data in the UI.
OneTime: If the
Mode is
OneTime then
data flows from source to target when application starts or datacontext changes.
 |
OneTime Binding Mode |
You can use this to show read only data. This can be used to
compare the changes occurred in UI. This
Mode very useful source doesn't implement
INotifyPropertyChanged Interface.
OneWayToSource: This is completely opposite to
OneWay mode. In this
Mode if target UI value is changed by user then it is updated in the source if the source is
dependency property or it is implemented by
INotifyPropertyChanged Interface.
 |
OneWayToSource Binding Mode |
In this case
target is not updated if source is updated because of changes occurred in target.
TwoWay: If the
Mode is
TwoWay then
Whenever the Source is updated then target is updated, in the same manner if the target value is changed by user then Source is updated with target value and If the binding Source is bind to multiple targets then all the targets are updated with Source data.
 |
TwoWay Binding Mode |
Use this Mode when User can change binding source and all it's targets.
For Understanding
Binding Modes briefly read this
Binding Modes in WPF with example
We have seen Binding Source is updated in
OneWayToSource and
TwoWay Binding Modes. More Precisely we can control the binding source updation by using
UpdateSourceTrigger.
UpdateSourceTrigger in WPF:
UpdateSourceTrigger is one of the property of Binding class. This Property defines the timing of binding source updation. This Property has three Options available. These are
LostFocus, PropertyChanged, Explicit.
Suppose you bind a property to a textbox, then if
UpdateSourceTrigger is set to
LostFocus then all the changes occurred in the textbox is updated in the source when TextBox Lost focus. If
UpdateSourceTrigger is set to
PropertyChanged then binding source is updated for every change in target. and if the
UpdateSourceTrigger is
Explicit then binding source is updated only when we explicitly called
UpdateSource ( ) method.
Conclusion:
I hope now you will able to
work with wpf bindings.
Download the Source Code and watch the Video about Bindings in
WPF. Let me know if you need any clarification.
Don't forget to Comment and share for our friends if you like this post.