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}">

  • 相关阅读:
    .net中对象序列化技术
    new、virtual、override
    如何检测指定的Windows服务是否启动
    c#中的GetUpperBound,GetLowerBound方法
    C#.net中的rank方法
    实现MD5算法
    用C#实现DES加密解密封装
    [转]JSP或servlet中(以及上传下载文件)中文乱码或不显示的解决方案
    [转]runOnUiThread 、 Handler 对比(一)
    [转]使用popBackStack()清除Fragment
  • 原文地址:https://www.cnblogs.com/tianciliangen/p/4317650.html
Copyright © 2011-2022 走看看