Simple binding of one or two elements
The binding of WPF elements, which refers to tying two controls together, such as using a slider slider to change the scale, increases the size of the corresponding label element's font.
The binding of this element, to know who is the target element, who is the source element. For example, use the scale size of the slider bar to bind the size of the font in the LABEL element. The scale of the slider is the source element, and the label font size is the target element.
In the label element in the font size of the attribute can be directly bound, binding statements are:
Fontsize= "{Binding elementname=slider1,path=value}
Explanation: Binding
elementname The name of the element that needs to be bound, and
Path needs to bind the attributes of the element
The complete code
<window x:class= "Bandingwpf.window1" xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" Title= "Window1" height= "560" width= "522.5" > <Grid> <stackpanel horizontalalignment= "left" height= "388" margin= "53,31,0,0" verticalalignment= "Top" width= "373" > <slider name= "Slider1" Horizontalalignment= "left" margin= "ten" verticalalignment= "Top" height= "width=" 353 "maximum=" value= "10" Tickplacement= "TopLeft" tickfrequency= "1"/> <textblock width= "Auto" name= "TextBlock1" Horizontalalignment= "left" margin= "ten" textwrapping= "Wrap" text= "TextBlock11" verticalalignment= "Top" FontSize = "{Binding elementname=slider1,path=value}"/> </StackPanel> </Grid></Window>
effect
Two, multiple elements of the binding
The above example then adds a text box element, and when the slider is not only changing the font size of the label element, it also displays the size of the font in the text box element, and the font size of the label box changes when the font size is entered directly into the text box element.
The simple example above only implements the source element to change the target element, and this example increases the target element to change the source element, the target element is the content of the text box, and the source element is the font size of the label. If the target element changes the source element, an attribute "updatesourcetrigger=propertychanged" needs to be added to the binding statement;
Full code
<window x:class= "Bandingwpf.window1" xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" XM lns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" title= "Window1" height= "587.5" width= "> <grid" ; <stackpanel horizontalalignment= "left" height= "388" margin= "ten" verticalalignment= "Top" width= "Auto" > &L T Slider name= "Slider1" horizontalalignment= "left" margin= "ten" verticalalignment= "Top" height= "width=" "353" maximum= "Value=" tickplacement= "TopLeft" tickfrequency= "1"/> <textblock width= "Auto" name= "TextBlock1" Ho Rizontalalignment= "left" margin= "ten" textwrapping= "Wrap" text= "TextBlock11" verticalalignment= "Top" fontsize= "{ Binding Elementname=slider1,path=value} "/> <textbox height=" "textwrapping=" Wrap "text=" {Binding Elem entname=textblock1,path=fontsize,updatesourcetrigger=propertychanged} "/> </StackPanel> </grid>&l T;/window>
Summarize
The binding of WPF elements, as a small application, can also be extended to choose colors to modify the color of the font and so on.
Bindings for WPF elements