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

  • 相关阅读:
    【 数据结构(C语言)】栈的应用——行编辑程序
    【 数据结构 (C语言)】栈的应用(二)——括号匹配问题
    节点
    页面加载--延迟加载
    雅黑php 探针
    Swiper 触屏滑动切换
    tab 选择悬停展示
    翻牌抽奖功能讲解
    公告信息滚动功能
    织梦提交表单不进行跳转
  • 原文地址:https://www.cnblogs.com/cw_volcano/p/3684188.html
Copyright © 2011-2022 走看看