WPF series-simple binding learning and wpf series binding
1. bind to an element object. (It is of little use in actual projects)
Bind two associated controls on the interface, such as the FontSize of a TextBlock and the Value of a Slider:
<Slider Name="sliderFontText" Minimum="1" Maximum="100" Value="10"/> <TextBox Name="txtValue" Width="200" Text="{Binding ElementName=sliderFontText, Path=Value,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
The Text content is a new Binding object, and ElementName, Path, and so on are bound property values.
C # code corresponding to XAML:
Binding binding = new Binding();binding.ElementName = "sliderFontText";binding.Path = new PropertyPath("Value");binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;binding.Mode = BindingMode.TwoWay;txtValue.SetBinding(TextBox.TextProperty, binding);
Delete binding:
BindingOperations.ClearBinding(txtValue, TextBox.TextProperty);
You can use the static method of System. Windows. Data. BindingOperations to delete binding and obtain control binding objects.
Description of BandingModel enumerated values:
Name |
Description |
OneWay |
Source property> target property (source change causes target change) |
TwoWay |
Source attribute <-> Target attribute (source changes lead to changes in the target and source changes also lead to changes in the target) |
| OneTime |
First run the source attribute> Target attribute, and all subsequent changes are ignored (unless BindingExpress. UpdateTarget () is called or completely different objects are re-bound ). |
OneWayToSource |
Opposite to OneTime binding |
| Default |
Dependency binding target attributes |
UpdateSourceTrigger enumeration values:
Name |
Description |
PropertyChanged |
Target attribute change-> Update source now |
LostFocus |
Target attribute changes + target loss of focus-> Update Source |
Explicit |
Call BindingExpression. UpdateSource ()-> Update Source |
Default |
Most of them are PropertyChanged, and Textbox. Text is LostFocus. |
2. bind non-interface elements
1. Bind static attributes of the static class (the system predefines the same as the custom method. Here we will demonstrate a custom attribute ):
XAML (c is the alias of The namespace WPFDemo ):
<Button Content="{Binding Source={x:Static c:MyRes.Name}}"/>
C #:
Namespace WPFDemo
{
Public static class MyStaticRes
{
Public static string Name {get {return "Returned Name ";}}
}
}
2. Bind The General attributes of a general class
XAML: create a resource object first,
Create a resource object:
<Window. Resources> <c: MyRes x: Key = "customRes" Name = "Custom Resource text"> </c: MyRes> </Window. Resources>
XAML binding:
<Button Content="{Binding Source={StaticResource customRes},Path=Name}"/>
C #:
namespace WPFDemo{ public class MyRes { public string Name { get; set; } }}
3. Relatively bound
XAML (bind the Name of StackPanel to the Content attribute of the Button here ):
<StackPanel x:Name="LayoutRoot"> <Button Content="{Binding Path=Name, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type StackPanel}}}"/> </StackPanel>
Model enumeration value of RelativeSource:
| Name |
Description |
| Self |
Bind another property of the user |
| FindAncestor |
To bind to a parent element, you must set the parent element type AncestorType and parent Element Level AncestorLevel (1 by default) |
| Previusdata |
The first item bound to the data binding list |
| TemplateParent |
Elements bound to an Application Template |
4. DataContext binding
<Button Content="{Binding Path=Name}" DataContext="{Binding Source={StaticResource myRes}}"/>
Or
<StackPanel Name="MainLayout" DataContext="{Binding Source={StaticResource myRes}}"> <Button Content="{Binding Path=Name}" /> </StackPanel>Binding attributes
Attribute |
Description |
ElementName |
It refers to the bound Source Element (this generally refers to the interface element) |
Path |
Source Element Binding Association attributes |
UpdateSourceTrigger |
Time to update bound attributes |
Model |
Update binding method |
| Source |
Provide data reference |
| RelativeSource |
Use a RelativeSource object to direct to the source object, mainly used for binding to the data source |
| DataContext |
The most important binding. Without Source or RelativeSource, WPF searches for the DataContext Attribute Based on the element tree and uses the first non-empty DataContext attribute. |
Here, the basic Banding has been reviewed. Here, one person will be: xaml sometimes forgets the syntax. In fact, XAML is also composed of objects one by one, when the composition method is used, New objects are created one by one. When the instantiation method is used: {object class property 1 = XX, property 2 = XX }.
Demo project download: http://files.cnblogs.com/files/zhaoxixi/WPFDemo.rar