Binding mode oneway wpf - Weebly

Continue

Binding mode oneway wpf

In my previous article I have discussed about the data binding in WPF and how we can bind the data provided to the UI elements of WPF. In this article I will walk you through the data binding modes present in WPF with sample code examples. Binding To Another Element In my previous article I have used a CLR class as data source to bind the Data to Target which is TextBox Control. There can be chances in our project where we want to bind the data of a control to other control. In that case element binding is used as shown in the below code snippet If we set some value in the TextBox the slider is automatically set to the value as shown in the figure below, as the default binding mode for the TextBox Text property is TwoWay which we can set while registering the dependency property. Binding Modes Though we can get the definition of the binding modes definition from msdn here. But in this part of the article I will discuss all of these modes with the practical examples of each of them. One Way ? This is the mode in which changes are propagated only from source to target and not the other way round. One example I can state here is that suppose I have many comboboxes in my window and on click of reset button I want to reset the SelectedIndex of each of these comboboxes which I will do using a common property, but this property should not be updated on selection of any of the combobox , because if changed then it will change the SelectedIndex for all the combobox. One Way To Source ? This is the mode in which source is updated with the data from the UI. One example which I can think of this scenario is the Login form where we need to send the data from the form to the model class. Two Way ? This is the binding in which we the target and source will be changed on change of source property or target property as shown in the above example of binding to another element. Default ? This is the binding mode which is specified while creating a dependency property . For example the binding mode for the Text property of the textbox control is Two Way. In the below code snippet I have created a MyProperty and set the Binding mode as Two way while registering public int MyProperty { get { return (int)GetValue(MyPropertyProperty); } set { SetValue(MyPropertyProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(int), typeof(MainWindow), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); One Time ? This mode only updates the target only once when the application starts or the data context is changed. This mode is useful if we have some static data. In this article I have discussed about the types of data binding in WPF and their use and utilization along with code examples. Top career enhancing courses you can't missMy Learning ResourceExcel your system design interview Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface. When a binding is established and the data or your business model changes, then it reflects the updates automatically to the UI elements and vice versa. It is also possible to bind, not to a standard data source, but to another element on the page. Data binding is of two types - one-way data binding and two-way data binding. One-Way Data Binding In one-way binding, data is bound from its source (that is the object that holds the data) to its target (that is the object that displays the data) Let's take a simple example to understand one-way data binding in detail. First of all, create a new WPF project with the name WPFDataBinding. The following XAML code creates two labels, two textboxes, and one button and initializes them with some properties. _Name: _Age: The text properties of both the textboxes bind to "Name" and "Age" which are class variables of Person class which is shown below. In Person class, we have just two variables Name and Age, and its object is initialized in MainWindow class. In XAML code, we are binding to a property Name and Age, but we have not selected what object that property belongs to. The easier way is to assign an object to DataContext whose properties we are binding in the following C# code in MainWindowconstructor. using System.Windows; namespace WPFDataBinding { public partial class MainWindow : Window { Person person = new Person { Name = "Salman", Age = 26 }; public MainWindow() { InitializeComponent(); this.DataContext = person; } private void Button_Click(object sender, RoutedEventArgs e) { string message = person.Name + " is " + person.Age; MessageBox.Show(message); } } public class Person { private string nameValue; public string Name { get { return nameValue; } set { nameValue = value; } } private double ageValue; public double Age { get { return ageValue; } set { if (value != ageValue) { ageValue = value; } } } } } Let's run this application and you can see immediately in our MainWindow that we have successfully bound to the Name and Age of that Person object. When you press the Show button, it will display the name and age on the message box. Let's change the Name and Age in the dialog box. If you now click the Show button, it will again display the same message. This because data binding mode is set to one-way in the XAML code. To show the updated data, you will need to understand two-way data binding. Two-Way Data Binding In two-way binding, the user can modify the data through the user interface and have that data updated in the source. If the source changes while the user is looking at the view, you want the view to be updated. Let's take the same example but here, we will change the binding mode from One Way to Two Way in the XAML code. _Name: _Age: Let's run this application again. It will produce the same output - Let's now change the Name and Age values - If you click the Show button now, it will display the updated message. We recommend that you execute the above code with both the cases for a better understanding of the concept.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download