6.3.4 Binding without Path
If the Binding source is data and does not require Path, you only need to set the Path value. In the XAML code, this "." can be omitted but not written, but cannot be omitted in the C # code.
[Html]
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<TextBlock Text = "{DynamicResource ResourceKey = str1}"/>
</Grid>
<Window. Resources>
<Sys: String x: Key = "str1"> the bodhi tree has no trees, and the mirror is not a platform. </Sys: String>
</Window. Resources>
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<TextBlock Text = "{DynamicResource ResourceKey = str1}"/>
</Grid>
<Window. Resources>
<Sys: String x: Key = "str1"> the bodhi tree has no trees, and the mirror is not a platform. </Sys: String>
</Window. Resources>
[Html]
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<TextBlock x: Name = "textBlock1"/>
</Grid>
<Window. Resources>
<Sys: String x: Key = "str1"> the bodhi tree has no trees, and the mirror is not a platform. </Sys: String>
</Window. Resources>
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<TextBlock x: Name = "textBlock1"/>
</Grid>
<Window. Resources>
<Sys: String x: Key = "str1"> the bodhi tree has no trees, and the mirror is not a platform. </Sys: String>
</Window. Resources>
[Csharp]
Var res = this. FindResource ("str1") as string;
This. textBlock1.SetBinding (TextBlock. TextProperty, new Binding (".") {Source = res });
Var res = this. FindResource ("str1") as string;
This. textBlock1.SetBinding (TextBlock. TextProperty, new Binding (".") {Source = res });
6.3.5 methods for specifying Source for Binding
Specify a single object of the common CLR type as Source. If the type implements the INotifyPropertyChanged interface, you can notify the Binding data to be updated by initiating the PropertyChanged event in the set statement of the attribute.
Specify a common CLR collection type object as Source. Generally, the ItemsSource attribute of the control is associated with a collection object using Binding.
Specify the ADO. Net data object as Source.
Use XMLDataProvider to specify XML data as Source.
Specify the Dependency Object as Source.
Specify the container's DataContext as Source.
Use ElementName to specify Source (used in XAML ).
Use the RelativeSource attribute of Binding to specify the Source.
Specify the ObjectDataProvider object as Source.
Use the data objects retrieved by using LINQ as the Binding source.
6.3.5 Binding without Source -- use DataContext as the Binding Source
When a Binding only knows its own Path and does not know its own Source, it will find it along the UI element tree to the root of the tree, every time a node passes by, we need to check whether the DataContext of this node has the attribute specified by Path. If yes, use this object as your source. If no, continue searching. If no Source is found at the root of the tree, the Binding has no source, therefore, no data is obtained.
[Html]
? <Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<TextBox x: Name = "textBox1" Grid. Row = "0" BorderBrush = "Black" Text = "{Binding Path = Age}"/>
<TextBox x: Name = "textBox2" Grid. Row = "2" BorderBrush = "Black" Text = "{Binding Path = Id}"/>
<TextBox x: Name = "textBox3" Grid. Row = "4" BorderBrush = "Black" Text = "{Binding Path = Name}"/>
<Grid. DataContext>
<Obj: Student Age = "20" Id = "100" Name = "Anders"/>
</Grid. DataContext>
</Grid>
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
<RowDefinition Height = "5"/>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<TextBox x: Name = "textBox1" Grid. Row = "0" BorderBrush = "Black" Text = "{Binding Path = Age}"/>
<TextBox x: Name = "textBox2" Grid. Row = "2" BorderBrush = "Black" Text = "{Binding Path = Id}"/>
<TextBox x: Name = "textBox3" Grid. Row = "4" BorderBrush = "Black" Text = "{Binding Path = Name}"/>
<Grid. DataContext>
<Obj: Student Age = "20" Id = "100" Name = "Anders"/>
</Grid. DataContext>
</Grid>
When the Source of the Binding is data and no attribute is required to expose data, the Path of the Binding can be set to "." Or omitted without writing. Now the Source can also be omitted without writing.
[Html]
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<TextBox x: Name = "textBox1" Grid. Row = "0" BorderBrush = "Black" Text = "{Binding Mode = OneWay}"/>
<Grid. DataContext>
<Sys: String> westward journey </sys: String>
</Grid. DataContext>
</Grid>
<Grid>
<Grid. RowDefinitions>
<RowDefinition Height = "25"/>
</Grid. RowDefinitions>
<TextBox x: Name = "textBox1" Grid. Row = "0" BorderBrush = "Black" Text = "{Binding Mode = OneWay}"/>
<Grid. DataContext>
<Sys: String> westward journey </sys: String>
</Grid. DataContext>
</Grid>