zoukankan      html  css  js  c++  java
  • WPF学习笔记

    Binding除了默认构造函数外,还有一个可以传入Path的构造函数,下面两种方式实现的功能是一样的。

    <TextBlock x:Name="currentFolder" DockPanel.Dock="Top"

                        Text="{Binding ElementName=treeView, Path=SelectedItem.Header}"

                        Backgroud="AliceBlue" FontSize="16" />

    <TextBlock x:Name="currentFolder" DockPanel.Dock="Top"

                        Text="{Binding SelectedItem.Header, ElementName=treeView}"

                        Backgroud="AliceBlue" FontSize="16" />

    这里使用ElementName来设置源对象,而没有使用Source属性来设置,这两种设置都是有效地,但在XAML里ElementName使用起来更方便,只需要给源元素一个名称就可以了,但如果要设置Source属性,目标对象必须被定义为某个ResourceDictionary中的资源,比如:

    <TextBlock x:Name="currentFolder" DockPanel.Dock="Top"

                        Text="{Binding Source={StaticResource treeView}, Path=SelectedItem.Header}"

                        Backgroud="AliceBlue" FontSize="16" />

    另一种指定数据源的方式是使用Binding的RelativeSource 属性,它通过与目标元素的关系获得相应的元素。RelativeSource的类型是RelativeSource,是一个标记扩展,有以下几种使用的方式:

    1. 使源元素为目标元素本身

    {Binding RelativeSource={RelativeSource self}}

    2. 使源元素为目标元素的TemplatedParent属性

    {Binding RelativeSource={RelativeSource TemplatedParent}}

    3. 使源元素为最近的指定类型的父元素

    {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type desiredType}}}

    4. 使源元素为n层最近的指定类型的父元素

    {Binding RelativeSource={RelativeSource FindAncestor, AncestorLevel=n, AncestorType={x:Type desiredType}}}

    5. 使源元素为之前的数据绑定集合中的数据项

    {Binding RelativeSource={RelativeSource PreviousData}}

    在RelativeSource 中使用Self是很方便的,因为这种模式可以把该元素的一个属性绑定到另一个属性上,但却不需要为元素指定名称,比如下面这个例子,Slider的ToolTip绑定了它自己的值:

    <Slider ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=Value}">

    源文地址:http://blog.csdn.net/cs_oldhorse/article/details/6817068

  • 相关阅读:
    《20171101-构建之法:现代软件工程-阅读笔记》
    《软件工程课程总结》
    《20171122-构建之法:现代软件工程-阅读笔记》) (5分)
    阅读任务-阅读提问-4
    《20171115构建之法:现代软件工程-阅读笔记》)
    对软件工程的期望
    自我介绍
    Javaweb学习计划
    分布式事务解决方案
    countdown模式
  • 原文地址:https://www.cnblogs.com/cw_volcano/p/3684188.html
Copyright © 2011-2022 走看看