WPF Binding Profile

Binding 1. Introduction

Data binding is a relationship. That is to extract information from the source object, the information provided to the properties of the target object.

Binding is divided into four parts: the source object, the source property binding target, target property. When the attribute is inconsistent source and target properties converters need to use the data conversion. Target property is always a dependency property, the source object can be anything.

2. Bind way

WPF non-binding way into two: create a binding markup language bindings and code.

Clear complete a simple binding, created when binding: Binding Markup Language <TextBlock x: Name = "textBolck" Text = "{Binding Source = ContentName, Path = Name1}" />

Source: source object

Path: Source Properties

TextBlock: binding target

Text: target attribute

Code bindings: You can remove dynamic binding and binding

 

Binding binding=new Binding();

binding.source = ContentName; // set the binding source

binding.Path = new PropertyPath ( "Name1"); // Set source properties

binging.Model = BindingModel.TwoWay; //// set the binding mode

textBolck.setBinding (TextBlock.Text, binding); // target binding properties of binding target

3. Bind error

Not throw an exception to notify the WPF data binding problem, even if the specified element or attribute does not exist, it will not be prompted by the abnormal, but can not display data in the target properties. Only when debugging an application, the exception information is displayed in the Output window of VS

 

4. binding mode

 

 

Binding mode setting data for controlling the source object and the refresh target properties. The five different modes

  1. OneWay: when the source property changes, update the target attribute
  2. TwoWay: update target property when the source property changes, and when the update source attributes target property changes
  3. OneTime: original source and destination attribute binding properties, followed by all the changes will be ignored
  4. OneWayToSource: and oneWay Similarly, when the direction opposite
  5. Default: Such binding depends on the target properties, either bi-directional, it can be one-way, unless specified mode, this mode is otherwise bind

5. Binding Update

When the data changes, the data may be notified in accordance with the new conditions of binding with the new. With the new conditions into four

  1. PropertyChanged when the target property changes immediately update source (live update)
  2. LostFocus: when the target property changes and when focus is lost on the way with a new source (TextBox with new data)
  3. Explicit: unless you call BindingExpression.UpdateSource () method, or can not update source
  4. Default: metadata data sources to determine the target update behavior, most default default when PropertyChanged, TextBox.Text property LocatFoucs

6. Binding delayed

The need to prevent data binding triggers the operation and modify the source object, the source object data binding processor-intensive operations performed when a property changes, in this case, you may want to add a short delay to avoid frequent trigger actions (such as when you click a button)

<Button x:Name="textBolck" Text="{Binding Source=ContentName,Path=Name1, Delay=100}",/>

7. binding source property

1. Binding element object: ElementName, the source object to the specified ElementName Name, Source Path attribute specified for the attribute
<TextBlock Text = "{Binding ElementName = button, Path = Text}" />

2. Bound to non-target elements

a.Source: This attribute references directly to the source object, the source object and the object to provide
    <Window.Resources>

        <FontFamily x:Key="sourBind">SourceBind</FontFamily>

<Window.Resources>

      <TextBlock Text="{Binding Source={StaticResource sourBind},Path=Source}"/>

b.RelativeSource: This is a reference to an object using the specified source object RelativeSource
    <TextBlock Text = "{Binding Name1 , RelativeSource = {RelativeSource Mode = FindAncestor, AncestorType = {x: Type Window}}}" />

c.DataContext: WPF element from the current element tree lookup to find the start up, check the genus DataContext each source, and uses the first non-null DataContext property

8. Remove the bind

1. The method can be adapted to two static class BindingOperation removed by binding codes

  clearBinding () method uses the dependency property (which has a binding property you want to delete) reference as an argument
  to remove all bindings ClearAllBinding ()

2. Manually bind to other data binding or set to null.

 

 

 

Guess you like

Origin www.cnblogs.com/qlbky/p/12014946.html