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

  • 相关阅读:
    安装VCSA6.5(vCenter Server Appliance 6.5)
    VMware文章总结
    kbmmw 的远程桌面功能2-android手机端
    kbmmw 的远程桌面功能
    delphi 中的win32 以外到平台的字符串处理一定慢吗?(转载)
    delphi 中如何访问另一个类中到私有方法?(转载)
    使用 kbmmw 的ORM开发纯REST数据库访问服务
    kbmmw 5.05.00 发布
    使用delphi-cross-socket 开发kbmmw smart http service
    利用Delphi-cross-socket 库提升kbmmw 跨平台开发
  • 原文地址:https://www.cnblogs.com/cw_volcano/p/3684188.html
Copyright © 2011-2022 走看看