指向源对象的引用(提供数据的对象)——编写代码,或者从静态资源获取
根据相对于目标对象的关系,指向源对象。
将一个元素绑定到自身或者父元素。
大量元素绑定到同一个对象。
在上层对象中,使用DataContext属性。 上几层无所谓。
<Window x:Class="Demo.WPFLearning.Bindingg.BindNoElementObjects" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:Demo.WPFLearning.Bindingg" mc:Ignorable="d" Title="BindNoElementObjects" Height="350" Width="400"> <Window.Resources> <FontFamily x:Key="CustomFont1"> Calibri</FontFamily> <FontFamily x:Key="CustomFont2">Seria</FontFamily> </Window.Resources> <Grid> <StackPanel Margin="3" Name="StackPanel" DataContext="{x:Static SystemFonts.IconFontFamily}"> <TextBlock Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=Source}" Margin="3"/> <TextBlock Text="{Binding Source={StaticResource CustomFont1},Path=Source}" Margin="3"/> <TextBlock > <TextBlock.Text> <Binding Path="Name"> <Binding.RelativeSource> <RelativeSource Mode="FindAncestor" AncestorType="{x:Type StackPanel}" AncestorLevel="1"/> </Binding.RelativeSource> </Binding> </TextBlock.Text> </TextBlock> <TextBlock Margin="5" Text="{Binding Path=Title,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}}}"/> <TextBlock Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=Source}" Margin="3"/> <TextBlock Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=LineSpacing}" Margin="3"/> <TextBlock Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=FamilyTypefaces[0].Style}" Margin="3"/> <!--在上层设置dataContext 属性后,不需要再重新设置Source了,--> <TextBlock Text="{Binding Path=Source}" Margin="3"/> <TextBlock Text="{Binding Path=LineSpacing}" Margin="3"/> <TextBlock Text="{Binding Path=FamilyTypefaces[0].Style}" Margin="3"/> </StackPanel> </Grid> </Window>