WPF bindings Binding and schema

Source: Internet
Author: User

Binding is to bind the value of an object property to the properties of another object

1. Default Bindings

public class Company
{
public string Name {get; set;}
}

XAML Code

1<stackpanel x:name= "StackPanel" >
2 <textbox x:name= "txtname" text= "{Binding path=name}" ></TextBox>
3 <textbox x:name= "txtName2" text= "{Binding path=name}" ></TextBox>
4 <button click= "Button_Click" >Ok</Button>
5</stackpanel>
Back-End Code:
01namespace Deepxaml
02{
public partial class Mainwindow:window
04 {
Mcompany Company;
Public MainWindow ()
07 {
InitializeComponent ();
Mcompany = new company {Name = "Microsoft"};
Ten this.stackPanel.DataContext = Mcompany;
11}
private void Button_Click (object sender, RoutedEventArgs e)
13 {
MessageBox.Show (This.mCompany.Name);
Mcompany.name = "Sun";
16}
17}
18}

Let's change the value of the first text box to IBM and click the button

This result shows that when we bind to a normal UI, the front end (the target of the binding) changes when the source of the binding changes. But when we click the button for the second time, the following figure appears

2. Bidirectional binding

The background uses code to change the object's property values, but the value of the front-end bindings does not change. So how do you let the UI control update the values by notifying the UI control of the background property changes? We need to implement an interface, and now we're going to do the following transformation for company

Namespace Deepxaml
02{
public class Company:inotifypropertychanged
04 {
private string name;
public event PropertyChangedEventHandler PropertyChanged;
public string Name
08 {
The get {return name;}
set {
One name = value;
if (this. PropertyChanged = null)
13 {
this. Propertychanged.invoke (this,new PropertyChangedEventArgs ("Name"));
15}
16}
17}
18}
19}

Now let's take a look at the results of our click button, when the UI value has been automatically updated to the value of the object changed in the background sun.

The direction of 3.Binding

<textbox x:name= "Txtname" text= "{Binding path=name, mode=twoway}" ></TextBox>
<textbox x:name= "txtName2" text= "{Binding path=name, mode=oneway}" ></TextBox>

This can be set mode when binding.

OneWay: with OneWay binding, the data is changed from source to destination whenever the origin changes.

OneTime: The binding also sends data from the source to the destination, but it does so only if the application is started or when the DataContext changes, so it does not listen for change notifications in the source.

OneWayToSource: binding sends data from the destination to the source.

TwoWay: The binding sends the source data to the destination, but if the value of the target property changes, it is sent back to the source.

Default: binding mode According to the actual situation, if it is editable is twoway, only read is OneWay.

When the above example does not set mode, the default is defaults.

4. How to set the binding in code

<stackpanel x:name= "StackPanel" >
<textbox x:name= "Txtname" ></TextBox>

</StackPanel>

WPF bindings Binding and schema

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.