Resources Binding

1.Resource的种类

  • Application.Resources 项目公用资源
  • Window.Resources 窗体公用资源
  • Control.Resources控制公用资源
  • Grid.Resource表格公用资源
  • 等等,只有当前组件和它的子组件可以使用

2.资源的静态绑定

<UserControl>   
    <!--创建一个FontFamily的元素,值是隶书,名称是 fontContent-->
    <Control.Resources>
        <FontFamily x:Key="fontContent">隶书</FontFamily>
    </Control.Resources>
 <Grid>
   <!--调用静态资源fontContent ,将其赋予FontFamily-->
   <TextBlock Grid.Row="6" Text="数据绑定" FontFamily="{Binding Source={StaticResource fontContent}, Path=Source}"/>
 </Grid>
</UserControl>

3.动态资源的绑定

RelativeSource中Mode=FindAncestor的情况(寻找相对父类)

<Grid x:Name="test1">
     <Grid>
         <TextBlock  Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=2,AncestorType={x:Type Grid}},Path=Name}"/>
     </Grid>
</Grid>

RelativeSource中Mode=Self 绑定自己


<Grid>
    <TextBlock Grid.Row="8" Width="30" Background="red" Height="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Width}"/>
</Grid>

RelativeSource Mode=TemplatedParent 啥玩意儿?

此条目发表在Wpf分类目录。将固定链接加入收藏夹。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注