<StackPanel Name="StackPanelOut"> <StackPanel Name="StackPanelIn"> <!--绑定元素--> <Slider Name="sliderFont" Maximum="40" Minimum="10" Value="20" TickPlacement="BottomRight" TickFrequency="1" IsSnapToTickEnabled="True" Height="Auto" Margin="5" ></Slider> <!--将某一属性绑定到上一个元素(ElementName),绑定值(Path),绑定模式(默认单向)--> <Label FontSize="{Binding ElementName=sliderFont,Path=Value,Mode=TwoWay}" Content="{Binding ElementName=sliderFont ,Path=Value}"></Label> <!--将某一属性,绑定到非元素资源--> <Label Content="{Binding Source={x:Static SystemFonts.IconFontFamily}, Path=Source}"/> <!--绑定到静态资源--> <Label Content="{Binding Source={StaticResource MyFont}, Path=Source}"></Label> <!--绑定到自定义资源--> <Label> <!--绑定到父类的数据--> <Label.Content> <Binding Path="Title"> <!--绑定到父类的值--> <Binding.RelativeSource> <!--绑定模式,绑定的类型--> <RelativeSource Mode="FindAncestor" AncestorType="{x:Type Window}"/> </Binding.RelativeSource> </Binding> </Label.Content> </Label> <!--针对上一个的简写,AncestorLevel在元素树上向上查找的等级--> <Label Content="{Binding Path=Name,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type StackPanel}, AncestorLevel=2}}"/> <!--多元素,绑定同一数据时的处理(处理相同源)--> <Label Content="{Binding Source={x:Static SystemFonts.IconFontFamily}, Path=Source}"/> <Label Content="{Binding Source={x:Static SystemFonts.IconFontFamily}, Path=LineSpacing}"/> <Label Content="{Binding Source={x:Static SystemFonts.IconFontFamily}, Path= FamilyTypefaces[0].Style}"/> <!--将上面绑定的数据 保存在父级(也可以是更高的父级)的DataContext中--> <StackPanel DataContext="{x:Static SystemFonts.IconFontFamily}"> <!--空出Source,WPF会自动向上查找父级中DataContext不为null的数据,并应用。如果找不到或者错误,则不显示--> <Label Content="{Binding Path=Source}"/> <Label Content="{Binding Path=LineSpacing}"/> <Label Content="{Binding Path= FamilyTypefaces[0].Style}"/> </StackPanel> </StackPanel> </StackPanel>