Silverlight Data Binding OneWay TwoWay Asp.Net C# XAML
Introduction:
In this article,i am going explain about,what are the binding options and modes available in silverlight application.
Main:
Data binding is simply a process of displaying data to the end user.Silverlight contains a rich data binding platform
that will help us to fetching data in faster and using less code.
Databinding needs atleast the below two items,
1.UI elements, (refers UI element,element property)
2.A Collection Source (refers a datasource and its path)
Next we need to find a path/protocol for connecting the above two items,Normally we will use datacontext for mapping
data into UI elements.
The DataContext is the source of all entities mapped over a connection. It tracks changes that you made to all
retrieved entities and maintains an “identity cache” that guarantees that entities retrieved more than one time are
represented by using the same object instance.
Different Modes of Data Binding,
The direction of the flow of data in a data binding scenario is controlled by the Mode property of the Binding.
The data has flowed from the source to the target (the UI controls). However, it can also flow in the opposite direction,
that is, from the target towards the source. This way, not only can data binding help us in displaying data, but also in
persisting data.
The data flow from source to target can be classified into modes ONEWAY,TWOWAY,ONLY ONCE,
A OneTime binding should be the default for data that does not change when displayed to the user. When using this mode,
the data flows from source to target. The target receives the value initially during loading and the data displayed in
the target will never change. Quite logically, even if a OneTime binding is used for a TextBox, changes done to the data
by the user will not flow back to the source.
We should use a OneWay binding for binding scenarios in which we want an up-to-date display of data. Data will flow from source to target here also, but every change in the values of the source properties will propagate to a change of the displayed values. Think of a stock market application where updates are happening every second. We need to push the updates to the UI of the application.
<TextBlock x:Name="CurrentBalanceValueTextBlock"
Text="{Binding CurrentBalance, Mode=OneWay}" >
</TextBlock>
<TextBlock x:Name="CurrentBalanceValueTextBlock" Text="{Binding CurrentBalance, Mode=OneWay}" > </TextBlock> |
The TwoWay bindings can help in persisting data. The data can now flow from source to target, and vice versa. Initially, the values of the source properties will be loaded in the properties of the controls. When we interact with these values (type in a textbox, drag a slider, and so on), these updates are pushed back to the source object. If needed, conversions can be done in both directions.
Add a note hereThere is one important requirement for the OneWay and TwoWay bindings. If we want to display up-to-date values, then the INotifyPropertyChanged interface should be implemented. The OneTime and OneWay bindings would have the same effect, even if this interface is not implemented on the source. The TwoWay bindings would still send the updated values if the interface was not implemented; however, they wouldn’t notify about the changed values. It can be considered as a good practice to implement the interface, unless there is no chance that the updates of the data would be displayed somewhere in the application. The overhead created by the implementation is minimal.
<TextBlock x:Name="CurrentBalanceValueTextBlock"
Text="{Binding CurrentBalance, Mode=TwoWay}" >
</TextBlock>
<TextBlock x:Name="CurrentBalanceValueTextBlock" Text="{Binding CurrentBalance, Mode=TwoWay}" > </TextBlock> |
conclusion:
Hope this helps,
Happy coding.





















