WPF Binding Learning (iii)

Source: Internet
Author: User

http://blog.csdn.net/lisenyang/article/details/183121991. Two-way binding between a control and a control

WPF also supports controls as data sources,

 <textbox name= " txt_source   " width="  120   " horizontalalignment="  left   " ></TextBox> <textbox text=< Span style= "COLOR: #800000" > " {binding Elementname=txt_source,path=text}   " name="   Txt_target   " width="  120  "  horizontalalignment=  " left  "; 

The above code can implement the two-way binding between the controls, and we find that our binding data sources are not source, but elementname.

Of course we can also use code to implement

New Binding ("Text"this. Txt_source};             this. txt_target.setbinding (textbox.textproperty, binding);

Bind the data source using code bindings that we still use for source

2. Multi-level path

WPF supports multi-level paths, that is, we say "dot" in layman's way.

Let's change the code above.

this. txt_target.setbinding (textbox.textproperty, binding);

Note : Because the source and destination data cannot be matched, two-way binding is not possible.

We can see that the data we bind is the length property in the Text property, which is what we call a multilevel path.

3. Index Binding

We know that the collection type is an indexer (Indexer), also known as a parameter attribute. Since it is an attribute, indexers can also be used as path, for example, we want a textbox to display the 1th character of another textbox

New Binding ("text[1]") {Source =this. Txt_source,mode = Bindingmode.oneway};             this. txt_target.setbinding (textbox.textproperty, binding);

4. Collection Bindings

When using a collection or DataView as a data source, if we want to use its default element as a data source, we need to use the following syntax:

list<string> list =Newlist<string> () {"Dog Doll","2 dogs left ."}; Txt_list1.setbinding (Textbox.textproperty,NewBinding ("/") {Source = list, Mode =bindingmode.onetime}); Txt_list2.setbinding (Textbox.textproperty,NewBinding ("/[1]") {Source = List,mode =bindingmode.onetime}); Txt_list3.setbinding (Textbox.textproperty,NewBinding ("/length"{Source = list, Mode = Bindingmode.onetime});

The effect is as follows

If you want to bind a second element, you can use the point directly

list<string> list =Newlist<string> () {"Dog Doll","2 dogs left ."}; Txt_list1.setbinding (Textbox.textproperty,NewBinding ("[1]") {Source = list, Mode =bindingmode.onetime}); Txt_list2.setbinding (Textbox.textproperty,NewBinding ("[1]. [1]") {Source = List,mode =bindingmode.onetime}); Txt_list3.setbinding (Textbox.textproperty,NewBinding ("[1]. Length"{Source = list, Mode = Bindingmode.onetime});

If you want to set the subset of the elements in the path, you can use the multilevel slash method ("slash" down);

We create a type of relationship between provinces and cities

  Public  classProvince { Public stringName {Get;Set; }  PublicIlist<city> Citys {Get;Set; } }     Public classCity { Public stringName {Get;Set; }  PublicIlist<district> Districts {Get;Set; } }     Public classDistrict { Public stringName {Get;Set; } }

Then we use a multi-level slash to bind

list<province> list =NewList<province>()            {                NewProvince {Name="Hebei", Citys=NewList<city>                    {                        NewCity {Name="Handan", districts=Newlist<district>{NewDistrict {name="Hanshan District"} }                        } } } }; Txt_list1.setbinding (Textbox.textproperty,NewBinding ("/name") {Source = list, Mode =bindingmode.onetime}); Txt_list2.setbinding (Textbox.textproperty,NewBinding ("/citys/name") {Source = list, Mode =bindingmode.onetime}); Txt_list3.setbinding (Textbox.textproperty,NewBinding ("/citys/districts/name"{Source = list, Mode = Bindingmode.onetime});

Of course, you can also use multi-level "points" to bind

New Binding ("[0]". Name") {Source = list, Mode =new Binding ("[0]. Citys[0]. Name") {Source = list, Mode =new Binding ("[0]. Citys[0]. Districts[0]. Name") {Source = list, Mode = Bindingmode.onetime});

WPF Binding Learning (iii)

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.