WPF Study Notes (1): bind data elements to elements. wpf Study Notes
Preface
As a cainiao, I learned WPF for a while, but I didn't summarize it. After a semester, I found that many things have been forgotten. I still need to write down many things for future review.
Data Binding is widely used in events and can effectively reduce the amount of code. What is data binding? To put it simply, extract some information from the source object and use it to set the attributes of the target object. Note that the target attribute must be a Dependency Property ), the source object can be anything.
Data Binding can be divided into binding elements to elements and binding elements to non-element objects.
Bind element to XAML binding
First, let's look at a simple example.
<Grid> <Grid. rowDefinitions> <RowDefinition> </Grid. rowDefinitions> <Slider Name = "sldFontSize" Minimum = "8" Maximum = "36" verticalignment = "Center"> </Slider> <TextBlock Grid. row = "1" Name = "txbSampleText" FontSize = "{Binding ElementName = sldFontSize, Path = Value, mode = TwoWay} "HorizontalAlignment =" Center "> example text </TextBlock> </Grid>
You can change the text value by changing the value of the slide bar. Here, the XAML is used for data binding. ElementName = sldFontSize indicates that the bound source object is the slide bar above, path = Value indicates that the property of the source object is the Value of the slider, and Mode = TwoWay indicates that bidirectional binding is used. There are four binding modes
Enumeration value of BindingMode
OneWay |
Update the target attribute when the source attribute changes. |
TwoWay |
Update the target attribute when the source attribute changes, and update the source attribute when the target attribute changes. |
OneTime |
Initially, the target attribute is set based on the source attribute value, and other changes are ignored. Reduce overhead if necessary |
OneWayToSource |
Same as OneWay, but in the opposite direction |
Default |
This type of binding depends on the Target attribute, which can be bidirectional or unidirectional. The default value is |
Two-way binding requires a higher cost. Therefore, you need to select a reasonable binding mode.
Code binding
It is most efficient to declare a Binding expression by using Binding extension in the XAML tag, but sometimes we still need to use code to create a Binding.
Binding bd = new Binding();bd.Source = sldFontSize;bd.Path = new PropertyPath("Value");bd.Mode = BindingMode.TwoWay;txbSampleText.SetBinding(FontSizeProperty, bd);
The functions completed by the above Code are the same as those created by using XAML. But it is much more complicated. When do I need to use code to create a binding?
1. Create dynamic binding: If you want to modify the binding based on other runtime information or create different bindings based on the environment, it is more reasonable to use the code to create the binding.
2. Delete binding: If you want to delete the binding and set the attributes in common mode, you can use the ClearBinding () or ClearAllBinding () methods.
Of course, one element can be bound with multiple
<Grid> <Grid. rowDefinitions> <RowDefinition> </Grid. rowDefinitions> <Slider Name = "sldFontSize" Minimum = "8" Maximum = "36" verticalignment = "Center"> </Slider> <TextBlock Grid. row = "1" Name = "txbSampleText" FontSize = "{Binding ElementName = sldFontSize, Path = Value, Mode = TwoWay}" Foreground = "{Binding ElementName = txtForeground, path = Text} "HorizontalAlignment =" Center "> example Text </TextBlock> <TextBox Grid. row = "2" Name = "txtForeground" FontSize = "{Binding ElementName = sldFontSize, Path = Value, Mode = TwoWay}"> </TextBox> </Grid>
As follows:
At the same time, there is also the issue of when to update the binding. Most of the binding updates start at PropertyChanged, but note that the default mode of TextBox. Text is LostFocus.
PropertyChanged |
Update the source immediately when the target attribute changes. |
LostFocus |
Update source when the target attribute changes and the target loses focus |
Explicit |
The source is not updated unless the BindingExpression. UpdateSource () method is called. |
Default |
Determine the update Behavior Based on the metadata of the Target attribute (based on the FrameworkPropertyMetadata. DefaultUpdateTrigger attribute) |
Here we also need to make a reasonable choice based on the overhead.
Binding Delay
If you need to pause the update for a while, you can add a short Delay time to avoid too frequent triggering operations, such as adding Delay = 500 to the code, you can update the source object after the user stops inputting 500 milliseconds.
Bind to a non-Element Object
In a data-driven program, more binding expressions are used to extract data from invisible objects. The only requirement is that the information to be displayed must be stored in public attributes, in this case, you need to discard Binding. elementName attribute, instead use one of the following attributes
- Source: This attribute refers to the reference to the Source object.
- RelativeSource: Uses RelativeSource to indicate the source object. With this additional layer, you can build references based on the current element.
- DataContext: if the Source or RelativeSource attribute is not used to specify the Source, WPF searches for the current element from the element tree, checks the DataContext attribute of each element, and uses the first non-empty DataContext attribute.
Data Binding is very important in WPF technology. In the next article, I will give a more in-depth introduction to binding to non-element objects.
References: WPF programming book-use C #2012 and. NET4.5 (version 4)