The second part of WPF (2)

Source: Internet
Author: User

. 3 source and path of Binding

To enable the object as the Binding source to automatically notify the Binding that its property value has changed, you need to enable the class to implement the INotifyPropertyChanged interface and trigger the PropertyChanged event in the set statement of the property.

6.3.1 use the control as the Binding source and the Binding tag Extension


[Html]
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
</Grid. RowDefinitions>
<TextBox x: Name = "textBox1" BorderBrush = "Black" Grid. Row = "0"/>
<TextBox x: Name = "textBox2" BorderBrush = "Black" Grid. row = "2" Text = "{Binding Path = Text, ElementName = textBox1, Mode = OneWay}"/>
</Grid>

<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
</Grid. RowDefinitions>
<TextBox x: Name = "textBox1" BorderBrush = "Black" Grid. Row = "0"/>
<TextBox x: Name = "textBox2" BorderBrush = "Black" Grid. row = "2" Text = "{Binding Path = Text, ElementName = textBox1, Mode = OneWay}"/>
</Grid>
6.3.2 control Binding direction and data update

The attribute controlling the Binding data flow direction is Mode, and its type is BindingMode enumeration. BindingMode can be set to TwoWay, OneWay, OnTime, OneWayToSource, or Default. Here, the Default value indicates that the Binding mode is determined based on the actual situation of the target. For example, if it is editable (such as the Text attribute of TextBox), the Default mode adopts the bidirectional mode; if it is read-only (such as TextBlock. text.

The UpdateSourceTrigger type is UpdateSourceTrigger enumeration, which can be PropertyChanged, LostFocas, Explicit, or Default. Obviously, the Default value of TextBox is the same as that of LostFocus.


[Html]
<TextBox x: Name = "textBox1" BorderBrush = "Black" Grid. Row = "0"/>
<TextBox x: Name = "textBox2" BorderBrush = "Black" Grid. row = "2" Text = "{Binding Path = Text, ElementName = textBox1, Mode = OneWayToSource, UpdateSourceTrigger = PropertyChanged}"/>

<TextBox x: Name = "textBox1" BorderBrush = "Black" Grid. Row = "0"/>
<TextBox x: Name = "textBox2" BorderBrush = "Black" Grid. row = "2" Text = "{Binding Path = Text, ElementName = textBox1, Mode = OneWayToSource, UpdateSourceTrigger = PropertyChanged}"/>
6.3.3 Binding Path (Path)


[Html]
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
</Grid. RowDefinitions>
<TextBox x: Name = "textBox1" BorderBrush = "Black" Grid. Row = "0" TextChanged = "textbox#textchanged"/>
<TextBox x: Name = "textBox2" BorderBrush = "Black" Grid. Row = "2" Text = "{Binding Path = Text, ElementName = textBox1}"/>
<TextBox x: Name = "textBox3" BorderBrush = "Black" Grid. Row = "4"/>
<TextBox x: Name = "textBox4" BorderBrush = "Black" Grid. row = "6" Text = "{Binding Path = Text. length, ElementName = textBox1, Mode = OneWay} "/>
<TextBox x: Name = "textBox5" BorderBrush = "Black" Grid. Row = "8"/>
<TextBox x: Name = "textBox6" BorderBrush = "Black" Grid. row = "10" Text = "{Binding Path = Text [2], ElementName = textBox1, Mode = OneWay}"/>
<TextBox x: Name = "textBox7" BorderBrush = "Black" Grid. Row = "12"/>
<TextBox x: Name = "textBox8" BorderBrush = "Black" Grid. Row = "14"/>
<TextBox x: Name = "textBox9" BorderBrush = "Black" Grid. Row = "16"/>
</Grid>

<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
</Grid. RowDefinitions>
<TextBox x: Name = "textBox1" BorderBrush = "Black" Grid. Row = "0" TextChanged = "textbox#textchanged"/>
<TextBox x: Name = "textBox2" BorderBrush = "Black" Grid. Row = "2" Text = "{Binding Path = Text, ElementName = textBox1}"/>
<TextBox x: Name = "textBox3" BorderBrush = "Black" Grid. Row = "4"/>
<TextBox x: Name = "textBox4" BorderBrush = "Black" Grid. row = "6" Text = "{Binding Path = Text. length, ElementName = textBox1, Mode = OneWay} "/>
<TextBox x: Name = "textBox5" BorderBrush = "Black" Grid. Row = "8"/>
<TextBox x: Name = "textBox6" BorderBrush = "Black" Grid. row = "10" Text = "{Binding Path = Text [2], ElementName = textBox1, Mode = OneWay}"/>
<TextBox x: Name = "textBox7" BorderBrush = "Black" Grid. Row = "12"/>
<TextBox x: Name = "textBox8" BorderBrush = "Black" Grid. Row = "14"/>
<TextBox x: Name = "textBox9" BorderBrush = "Black" Grid. Row = "16"/>
</Grid>
[Csharp] view plaincopyprint? Private void textBox1_TextChanged (object sender, TextChangedEventArgs e)
{
Binding binding1 = new Binding ("Text") {Source = this. textBox1 };
BindingOperations. SetBinding (textBox3, TextBox. TextProperty, binding1 );
 
Binding binding2 = new Binding ("Text. Length") {Source = this. textBox1, Mode = BindingMode. OneWay };
This. textBox5.SetBinding (TextBox. TextProperty, binding2 );
 
List <string> stringList = new List <string> () {"Tim", "Tom", "Blog "};
This. textBox7.SetBinding (TextBox. TextProperty, new Binding ("[2]") {Source = stringList, Mode = BindingMode. OneWay}); // Blog
This. textBox8.SetBinding (TextBox. TextProperty, new Binding ("/[2]") {Source = stringList, Mode = BindingMode. OneWay}); // m
This. textBox9.SetBinding (TextBox. TextProperty, new Binding ("/") {Source = stringList, Mode = BindingMode. OneWay}); // Tim
}

Private void textBox1_TextChanged (object sender, TextChangedEventArgs e)
{
Binding binding1 = new Binding ("Text") {Source = this. textBox1 };
BindingOperations. SetBinding (textBox3, TextBox. TextProperty, binding1 );

Binding binding2 = new Binding ("Text. Length") {Source = this. textBox1, Mode = BindingMode. OneWay };
This. textBox5.SetBinding (TextBox. TextProperty, binding2 );

List <string> stringList = new List <string> () {"Tim", "Tom", "Blog "};
This. textBox7.SetBinding (TextBox. TextProperty, new Binding ("[2]") {Source = stringList, Mode = BindingMode. OneWay}); // Blog
This. textBox8.SetBinding (TextBox. TextProperty, new Binding ("/[2]") {Source = stringList, Mode = BindingMode. OneWay}); // m
This. textBox9.SetBinding (TextBox. TextProperty, new Binding ("/") {Source = stringList, Mode = BindingMode. OneWay}); // Tim
}

 

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.