The Silverlight binding engine can bind the attribute values between UI elements, and can also bind UI elements with CLR objects. However, there are two other binding methods that are easy to ignore ......
1. The UI element can also be bound to its own property value, for example, the following XAML code:
1: <!--ContentPanel - place additional content here-->
2: <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
3: <StackPanel Orientation="Horizontal"
4: HorizontalAlignment="Center"
5: VerticalAlignment="Center">
6:
7: <TextBlock Text="{Binding RelativeSource={RelativeSource Self},
8: Path=FontFamily}" />
9:
10: <TextBlock Text=" - " />
11:
12: <TextBlock Text="{Binding RelativeSource={RelativeSource Self},
13: Path=FontSize}" />
14:
15: <TextBlock Text=" pixels" />
16: </StackPanel>
17: </Grid>
18:
In the above Code, the first textblock binds the displayed text to the fontfamily attribute value on the inheritance tree, and the second textblock binds the displayed text to the fontsize attribute value on the inheritance tree, pay attention to the binding syntax here-use the relativesource attribute of the binding object, and its attribute value is represented in {relativesource self} Special syntax.
2. Another binding form is useful when "assembling" usercontrol.
When we "Assemble" usercontrol, we often need to customize attributes and events, and here the attributes and events are mostly "assembly components" -- (various controls constitute visual tree to form usercontrol) properties and events;
For example, to "Assemble" A textblock surrounded by borders, first use the C # Code to define dependency attributes:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Net;
5: using System.Windows;
6: using System.Windows.Controls;
7: using System.Windows.Documents;
8: using System.Windows.Input;
9: using System.Windows.Media;
10: using System.Windows.Media.Animation;
11: using System.Windows.Shapes;
12:
13: namespace MyBorderTextBlock
14: {
15: public partial class BorderTextBlock : UserControl
16: {
17: public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(BorderTextBlock), new PropertyMetadata("Hello,UserControl!"));
18: public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register("FontSize", typeof(int), typeof(BorderTextBlock), new PropertyMetadata(18));
19: public int FontSize
20: {
21: get
22: {
23: return (int)GetValue(FontSizeProperty);
24: }
25: set
26: {
27: SetValue(FontSizeProperty, value);
28: }
29: }
30: public string Text
31: {
32: get
33: {
34: return GetValue(TextProperty).ToString();
35: }
36: set
37: {
38: SetValue(TextProperty, value);
39: }
40: }
41: public BorderTextBlock()
42: {
43: InitializeComponent();
44: }
45: }
46: }
47:
48:
Then define the following XAML code:
1: <UserControl x:Class="MyBorderTextBlock.BorderTextBlock"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: Name="this"
5: >
6: <Border BorderBrush="#FFD83232" BorderThickness="10"
7: >
8: <TextBlock Width="500" Height="150"
9: Text="{Binding ElementName=this, Path=Text}"
10: FontSize="{Binding ElementName=this, Path=FontSize}"/>
11: </Border>
12: </UserControl>
13:
Note that we do not recommend defining the height and width when assembling the usercontrol. Otherwise, it is difficult to adjust the layout of the user control on the usercontrol page; in the above XAML code, the border height and width of the "assembly part" are determined based on the height and height of the child element textblock. In addition, the usercontrolname attribute value is defined as this, generally, this is recommended ...... Of course, you can also use other names. In fact, the most clever is this code: text = "{binding elementname = This, Path = text }", in this way, the text attribute of usercontrol is bound with the property of this "assembly part", and we can use the text attribute of usercontrol!