<Window x:Class="XamlTest.Window15"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window15" Height="300" Width="300">
    <Grid>
        <StackPanel Name="sp1">
            <Grid Name="gd1">
                <StackPanel Name="sp2">
                    <Grid Name="gd2">
                        <!--<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorLevel=1, AncestorType=StackPanel, Mode=FindAncestor}, Path=Name}"></TextBlock>-->//xaml实现
                        <TextBlock Name="txt"></TextBlock>
                    </Grid>
                </StackPanel>
            </Grid>
        </StackPanel>
    </Grid>

</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace XamlTest
{
    /// <summary>
    /// Interaction logic for Window15.xaml
    /// </summary>
    public partial class Window15 : Window
    {
        public Window15()
        {
            InitializeComponent();
            Binding b = new Binding();
            b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(StackPanel), 1);
            b.Path = new PropertyPath("Name");
            txt.SetBinding(TextBlock.TextProperty, b);
        }
    }
}

WPF RelativeSource的使用的更多相关文章

  1. [WPF系列-高级TemplateBinding vs RelativeSource TemplatedParent]

    What is the difference between these 2 bindings: <ControlTemplate TargetType="{x:Type Button ...

  2. 在WPF中如何使用RelativeSource绑定

    在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...

  3. WPF利用通过父控件属性来获得绑定数据源RelativeSource

    WPF利用通过父控件属性来获得绑定数据源RelativeSource   有时候我们不确定作为数据源的对象叫什么名字,但知道作为绑定源与UI布局有相对的关系,如下是一段XAML代码,说明多层布局控件中 ...

  4. WPF绑定のRelativeSource

    在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource. 这种办法的意思是指当前元素和绑定源的位置关系. 第一种关系: Self 举一个最简单的例子:在一个StackPanel ...

  5. 【WPF】WPF通过RelativeSource绑定父控件的属性

    1.后台代码实现绑定父控件的属性 RelativeSource rs = new RelativeSource(RelativeSourceMode.FindAncestor); //设定为离自己控件 ...

  6. WPF的控件Binding的ElementName/RelativeSource具体用法

    <TextBlock Name="_txtSickBedNo" FontStyle="Normal" Foreground="Black&quo ...

  7. RelativeSource.TemplatedParent 属性wpf

    今天看到这一句代码时候,自己只是知道绑定了,可是不知道绑定了什么啊 就去查了一下,后来说的好像是绑定的TemplateParent返回的 一个值.可是这是为什么呢, 有的说是绑定的是一个资源. 下面有 ...

  8. WPF绑定数据源之RelativeSource

    Command="{Binding ConfirmRegisterCommand}" CommandParameter="{Binding RelativeSource= ...

  9. WPF教程(四)RelativeSource属性

    我们进行Bingding时,如果明确知道数据源的Name,就能用Source或者ElementName进行绑定,但是有时候我们需要绑定的数据源可能没有明确的Name,此时我们就需要利用Bingding ...

随机推荐

  1. html5的float属性超详解(display,position, float)(文本流)

    html5的float属性超详解(display,position, float)(文本流) 一.总结 1.文本流: 2.float和绝对定位都不占文本流的位置 3.普通流是默认定位方式,就是依次按照 ...

  2. Swift基础1.1——基本的语法—变量和常量

    前些日子.第一届Swift开发人员大会开了之后.身边非常多搞OC的朋友就按捺不住了. 都认为是时候学一下Swift了,毕竟Swift已是趋势. 也是应他们再三要求,让我整理一下Swift的学习心得.今 ...

  3. 【u005】封锁阳光大学

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹 ...

  4. 日志框架logj的使用

    log4j 简介 是什么? Apache的一个开源的.轻量级的.用于日志管理的框架 有什么? Log4j由三个重要的组件构成:日志信息的输出格式,日志信息的优先 级,日志信息的输出目的地. 1,日志信 ...

  5. href="javascript:;" href="javascript:void(0);" href="#"区别

    一.href="javascript:;" 这种用法不正确,这么用的话会出现浏览器访问"javascript:;"这个地址的现象: 二.href="j ...

  6. nodejs版本号更新问题:express不是内部或外部命令

    版本号更新后,我们使用熟悉的npm install -g express命令安装,可是,成功安装之后竟然提示express不是内部或外部命令. nodejs小问题:[1]express不是内部或外部命 ...

  7. js 进阶 10 js选择器大全

    js 进阶 10 js选择器大全 一.总结 一句话总结:和css选择器很像 二.JQuery选择器 原生javaScript中,只能使用getELementById().getElementByNam ...

  8. javaScript实现简单网页倒计时代码

    <div id="button"> <input type="button" value="同意" id="b0 ...

  9. Archive for the ‘Erlang’ Category 《Erlang编程指南》读后感

    http://timyang.net/category/erlang/ 在云时代,我们需要有更好的能利用多核功能及分布式能力的编程语言,Erlang在这方面具有天生的优势,因此我们始终对它保持强烈关注 ...

  10. springboot 使用传统方式部署

    spring boot默认创建出来的应用程序是内嵌web容器的,直接运行jar文件就可以的,但通常我们也需要将程序部署到tomcat中,这需要做如下改进: 1.pom.xml修改 打包方式需要修改成w ...