WPF learning: Binding

Source: Internet
Author: User

I. Bind to Element Object

1. Elements and elements (XAML, code)

1.1xaml format

Target property = "{binding elementname = source control, Path = source property, mode = ...} "

1.2 code:

Binding binding = new binding ();
Binding. Source = source control;
Binding. Path = new propertypath ("value"); // source control Property
Binding. mode = bindingmode. twoway;
Lblsampletext. setbinding (textblock. fontsize, binding); bind.

1.3 demo program...

II. Bind to a non-Element Object (Source/relativesource/datacontext ).

1. Source

1.1 bind to a static object

Fontsize = "{binding source = {X: static object },

Path = static object attributes}

1.2 bind to a resource.

Fontfamily = "{binding source = {staticresource resource name },

Path = Resource object attributes }"

Demo...

2. Relativesource: automatically searches for and binds the source based on the current object.

2.1 = "{binding relativesource =

{Relativesource mode = findancestor,

Ancestortype = {X: type window }},

Path = title }"

2.2 relativesourcemode enumeration Value

Name

Description

Self

The expression is bound to another attribute of the same element (on the 10th

This section describes an example of this technology.

Displays the text associated with the command in the control that triggers the command)

Findancestor

The expression is bound to the parent element. WPF searches for the element tree

The expected parent element. To specify the parent element, you must also set

The ancestortype attribute indicates the type of the parent element to be searched.

In addition, you can also use the ancestorlevel attribute to skip the first detected

A specific number of elements. For example, when searching in a tree,

If you want to bind an element of the third listboxitem type,

The following settings should be performed: ancestortype = {X: Type listboxitem },

And ancestorlevel = 3, so the first two listboxitems are skipped.

Element. By default, the ancestorlevel attribute is set to 1,

And stop searching when the first matching element is found.

Previusdata

The expression is bound to the previous data item in the data binding list.

This mode is used in a list item.

Templateparent

The elements bound to the Application Template. Only when the binding is in

Control templates or data templates can work in this mode.

3. Datacontext

All sub-objects can share one binding source.

Parent object = datacontext = "{binding elementname = wndbindingtype }"

Sub-object = fontsize = "{binding Path = an attribute bound to the source }"

3. Binding direction

Bindingmode enumeration Value

Name

Description

Oneway

Update the target attribute when the source attribute changes.

Twoway

Update the target attribute when the source attribute changes,

The source attribute is updated when the target attribute changes.

Onetime

Initially, the target attribute is set based on the source attribute value. However,

All subsequent changes will be ignored (unless bound

Set to a completely different object or call

Bindingexpression. updatetarget () method,

As described later in this chapter ). Normally, if

You can use this mode to reduce the overhead because the source attribute does not change.

Onewaytosource

Similar to the onway type, but in the opposite direction. When the target

Update the source attribute when the attribute changes (this looks a bit backward ),

However, the target property will never be updated.

Default

This type of binding depends on the Target attribute. It can

Is bidirectional (for attributes that can be set by users, such

Textbox. Text attribute), can also be one-way (

All other attributes ). Unless another mode is explicitly specified,

Otherwise, all bindings use this mode.

Direction:

Figure 16-5 bind two attributes in different directions

5. Important

5.1 bindingoperations: clear binding, setting binding, and getting binding expressions

5.2 bindingexpression: updatesource () method and updatetarget () method.

5.2.1 get

Source code line number "onclick =" function onclick ()
{
Function onclick ()
{
Linenumbervisible (this)
}
} "Onmouseover =" function onmouseover ()
{
Function onmouseover ()
{
Changeicon (this, true)
}
} "Onmouseout =" function onmouseout ()
{
Function onmouseout ()
{
Changeicon (this, false)
}
} "> Hide row number {
Function onclick ()
{
Copycode (this)
}
} "Onmouseover =" function onmouseover ()
{
Function onmouseover ()
{
Changeicon (this, true)
}
} "Onmouseout =" function onmouseout ()
{
Function onmouseout ()
{
Changeicon (this, false)
}
} "Onkeypress =" function onkeypress ()
{
Function onkeypress ()
{
Copycode_checkkey (this, true)
}
} "> Copy the code {
Function onclick ()
{
Aboutme ()
}
} "Onmouseover =" function onmouseover ()
{
Function onmouseover ()
{
Changeicon (this, true)
}
} "Onmouseout =" function onmouseout ()
{
Function onmouseout ()
{
Changeicon (this, false)
}
} ">? This is a piece of program code.
  1. // Get the binding that's applied to the text box.  
  2. BindingExpression binding = txtFontSize.GetBindingExpression(TextBox.TextProperty);   
  3. // Update the linked source (the TextBlock).  
  4. binding.UpdateSource(); 

5.3 "X: static" values are taken from static objects and attributes based on the target object type. Return Value: Object/value.

6. Summary:

The attribute value must be changed: inotifypropertychanged

Inotify collectionchanged

The binding of an element to an element depends on the dependent attribute. When registering a dependency attribute, the property value change notification is sent.

4. Custom class

1. Update implementation

1.1 attribute dependency attributes

1.2 inotifypropertychanged

Five collection binding

Attributes used for data binding in the itemscontrol class (the only requirement is that the ienumerable interface is supported)

Name

Description

Itemssource

Point to a set that contains all objects displayed in the list

Displaymemberpath

Identifies an attribute used to create a display text for each item

Itemtemplate

A data template is used to create a visual appearance for each item.

This attribute is more powerful than the displaymemberpath attribute,

Itemscontrol (ListBox control, ComboBox control, and listview control (as well as Menu Control and Treeview control used to display hierarchical data )).

 

II. Value converter.

Ivalueconverter Interface Class

Convert bound data

 

<Binding Path = "code">

<Binding. converter>

<Local: priceconverter> </local: priceconverter>

</Binding. converter>

</Binding>

3. Use of data templates

Data templates are usually applied to the following controls to fill in the datatemplate attributes:

· Content control: the contenttemplate attribute controls content display.

· Item control (Items Control): itemtemplate attribute, applied to each displayed item

· Header content control: Specifies the headertemplate attribute to control the display of headers.

4. View

1. Filter

2. Sort

3. Group

5. Data Provider

1. Bound to an instance

1. objectdataprovider can bind an object with parameters in the constructor.

2. Can be bound to a method

 

<Local: bqitems X: Key = "bqs"/>
<Objectdataprovider X: Key = "dataproviderbq"
Objectinstance = "{staticresource bqs}">
</Objectdataprovider>

<Objectdataprovider X: Key = "dphasparameter" objecttype = "{X: type local: bqitems}">
<Objectdataprovider. constructorparameters>
<SYS: int32> 23 </sys: int32>
</Objectdataprovider. constructorparameters>
</Objectdataprovider>

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.