zoukankan      html  css  js  c++  java
  • WPF : Binding的3个属性: Source, RelativeSource, ElementName

    ElementName

    The ElementName property is used to reference an object by the name of the object. This is particularly useful in XAML, where you can directly reference other elements defined in XAML.

    举例

    <StackPanel Background="Blue">
    <Button x:Name="refButton" Background="Orange"/>
    <Button Background="{Binding ElementName=refButton, Path=Background}"/>
    </StackPanel>


     

    Source

    The Source property is used to specify an object reference on which the binding Path or XPath will be evaluated. The Source property is usually used when the object on which the Binding is set is known and differs from the DataContext.

    举例:

    <CustomClass1 Property1="{Binding Source={x:Static DateTime.Now}, Path=Day}"/>

    <CustomClass1 Property1="{Binding Source={StaticResource AnotherElement}, Path=ActualWidth}"/>

    RelativeSource

    Binding RelativeSource={
       
    RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemType}
    }
    The default attribute of RelativeSource is the Mode property. A complete set of valid values is given here (from MSDN):

    • PreviousData Allows you to bind the previous data item (not that control that contains the data item) in the list of data items being displayed.

    • TemplatedParent Refers to the element to which the template (in which the data-bound element exists) is applied. This is similar to setting a TemplateBindingExtension and is only applicable if the Binding is within a template.

    • Self Refers to the element on which you are setting the binding and allows you to bind one property of that element to another property on the same element.

    • FindAncestor Refers to the ancestor in the parent chain of the data-bound element. You can use this to bind to an ancestor of a specific type or its subclasses. This is the mode you use if you want to specify AncestorType and/or AncestorLevel.

    举例:

    元素的一个属性绑定在自身的另一个属性上

    {Binding Path=PathToProperty, RelativeSource={RelativeSource Self}}

    元素的一个属性绑定在父元素的属性上

    {Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}

    Template中的元素的属性绑定在Template使用者元素的属性上

    {Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}

    {TemplateBinding Path=PathToProperty}

  • 相关阅读:
    内嵌汇编简介(转)
    binary hacks读数笔记(dlopen、dlsym、dlerror、dlclose)
    在Ubuntu 20.04 LTS Focal Fossa上安装Fail2ban
    如何在CentOS 8上安装和配置Fail2ban
    Linux中使用Head命令的7种方法
    如何在CentOS / RHEL上安装Nginx
    如何在Fedora 32/31/30上安装NVM?
    如何在Ubuntu 20.04 LTS Focal Fossa上安装WildFly?
    如何在CentOS 8上安装oVirt虚拟机管理器
    如何在CentOS 8上安装CentOS Web面板?
  • 原文地址:https://www.cnblogs.com/mrfangzheng/p/1611045.html
Copyright © 2011-2022 走看看