当我们需要绑定到非元素对象时,我们就不能够使用元素的名称属性,只能使用以下三种属性中的一个。
1、Source属性
是指向源对象的引用,也就是数据的对象。
<Window x:Class="_24.Bingding_non_Elements.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<FontFamily x:Key="测试字体">雅黑字体</FontFamily>
</Window.Resources>
<Grid>
<StackPanel Margin="5">
<TextBlock Margin="5" Height="23" Name="textBlock1"
Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=Source}" />
<TextBlock Margin="5" Height="23" Name="textBlock2"
Text="{Binding Source={StaticResource 测试字体},Path=Source}" />
</StackPanel>
</Grid>
</Window>
我们将文本框的内容绑定为静态的对象,和提前加载的资源上,下面是结果:
2、RelativeSource属性
RelativeSource属性可以把相对于目标属性的关系指向源对象。
<Window x:Class="_24.Bingding_non_Elements.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<FontFamily x:Key="测试字体">雅黑字体</FontFamily>
</Window.Resources>
<Grid>
<StackPanel Margin="5">
<TextBlock Margin="5" Height="23" Name="textBlock1"
Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=Source}" />
<TextBlock Margin="5" Height="23" Name="textBlock2"
Text="{Binding Source={StaticResource 测试字体},Path=Source}" />
<TextBlock Margin="5" Height="23" Name="textBlock3">
<TextBlock.Text>
<Binding Path="Title">
<Binding.RelativeSource>
<RelativeSource Mode="FindAncestor" AncestorType="{x:Type Window}"></RelativeSource>
</Binding.RelativeSource>
</Binding>
</TextBlock.Text>
</TextBlock>
<TextBlock Margin="5" Height="23" Name="textBlock4"
Text="{Binding Path=Name, RelativeSource={RelativeSource Mode=Self}}" />
</StackPanel>
</Grid>
</Window>
我们可以通过 RelativeSource的关系设置访问目标对象的父元素,本身,同级的上一个元素等等对象
3、DataContext属性
有时候我们需要将大量的元素绑定到同一个对象。
如果每写一个元素就要声明一次绑定的对象,那样代码的重复性就会非常高,我们就可以在这些元素的上一级里面设置DataContext属性,将对象写进这个属性,然后元素里面只用说明绑定对象的哪个属性就可以了。
<Window x:Class="_24.Bingding_non_Elements.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<FontFamily x:Key="测试字体">雅黑字体</FontFamily>
</Window.Resources>
<Grid>
<StackPanel Margin="5" DataContext="{x:Static SystemFonts.IconFontFamily}">
<TextBlock Margin="5" Height="23" Name="textBlock1"
Text="{Binding Source={x:Static SystemFonts.IconFontFamily},Path=Source}" />
<TextBlock Margin="5" Height="23" Name="textBlock2"
Text="{Binding Source={StaticResource 测试字体},Path=Source}" />
<TextBlock Margin="5" Height="23" Name="textBlock3">
<TextBlock.Text>
<Binding Path="Title">
<Binding.RelativeSource>
<RelativeSource Mode="FindAncestor" AncestorType="{x:Type Window}"></RelativeSource>
</Binding.RelativeSource>
</Binding>
</TextBlock.Text>
</TextBlock>
<TextBlock Margin="5" Height="23" Name="textBlock4"
Text="{Binding Path=Name, RelativeSource={RelativeSource Mode=Self}}" />
<TextBlock Margin="5" Height="23" Name="textBlock5"
Text="{Binding ,Path=Source}" />
<TextBlock Margin="5" Height="23" Name="textBlock6"
Text="{Binding ,Path=LineSpacing}" />
<TextBlock Margin="5" Height="23" Name="textBlock7"
Text="{Binding ,Path=FamilyTypefaces[0].Style}" />
</StackPanel>
</Grid>
</Window>