zoukankan      html  css  js  c++  java
  • WPF 绑定方式Binding、TemplateBinding、TemplatedParent

    属性binding方式

    绑定到DataContext

    <!--绑定到DataContext-->
    <Button Content="{Binding DataTime}"/>
    <Button Content="{Binding RelativeSource={RelativeSource self},Path=DataTime}"/>
    
    <!--绑定到DataContext,并设置绑定模式-->
    <Button x:Name="btn" Content="{Binding DataTime,Mode=OneTime}"/>
    
    <!--绑定到DataContext,并设置更新模式-->
    <Button Content="{Binding DataTime,UpdateSourceTrigger=PropertyChanged}"/>
    
    <!--绑定到DataContext,并设置转换模式-->
    <Button Content="{Binding DataTime,Converter={StaticResource ConvertResource},ConverterParameter=btn1}"/>
    

    绑定到Element

    <!--绑定到Element中指定属性-->
    <Button Content="{Binding ElementName=btn,Path=Content}"/>
    
    <!--绑定到相对位置中的自身模式-->
    <Button Content="{Binding Tag, RelativeSource={RelativeSource Self}}" Tag="MyTag"/>
    

    绑定到父级别

    如果没有显式设置Mode属性,则设置AncestorType或AncestorLevel属性将隐式地将Mode属性值锁定为FindAncestor。

    <!--绑定到相对位置中的父级别查找模式 绑定到指定类型-->
    <Button Content="{Binding Content, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
    
    <!--绑定到相对位置中的父级别查找模式 绑定到指定层级-->
    <Button Content="{Binding Content, RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=2}}"/>
    <!--此TextBlock在ListBox第二层级,绑定第一层级(父级)的属性-->
    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorLevel=2, AncestorType={x:Type ListBoxItem}}, Path=DataContext.PropertyName1}"/>
    
    <!--绑定到相对位置中的父级别查找模式 绑定到模板内容 Template使用者元素的属性上-->
    <Button Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>
    
    <!--GroupBox.Heade自定义显示-->
    <GroupBox>
        <GroupBox.HeaderTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Header, RelativeSource={RelativeSource AncestorType={x:Type GroupBox},AncestorLevel=1}}" FontSize="12"/>
            </DataTemplate>
        </GroupBox.HeaderTemplate>
        <Grid x:Name="grdContent">
    	</Grid>
    </GroupBox>
    

    TemplateBinding与Binding

    TemplateBinding是Binding的一个轻量级版本,最主要的用途是内置在模板中绑定模板化元素的属性。
    Background="{TemplateBinding Foreground}"
    Background="{Binding Foreground, RelativeSource={RelativeSource TemplatedParent}}"

    TemplateBinding的数据绑定是单向的,从数据源到目标。
    TemplateBinding不能对数据对象进行自动转换,数据源和目标的数据类型不同时候,需要自己写转换器。

  • 相关阅读:
    poj 3074(DLX)
    zoj 3209(DLX)
    hdu 4531(模拟+BFS+DFS)
    hdu 2065(递推+矩阵优化)
    poj 3714 (最近点对)
    zoj 3690(递推+矩阵优化)
    poj 3076(DLX)
    hdu 4533(一种很巧妙的方法|线段树+扫描线)
    hdu 4513(模拟)
    sql server 2008 评估已过期。
  • 原文地址:https://www.cnblogs.com/wesson2019-blog/p/13152503.html
Copyright © 2011-2022 走看看