<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. php实现用短路求值原理求1+2+3+...+n(短路求值是什么)(仔细分析题干)

    php实现用短路求值原理求1+2+3+...+n(短路求值是什么)(仔细分析题干) 一.总结 1.仔细分析题干,找出要点:该递归还是得递归啊 2.短路求值原理:&&就是逻辑与,逻辑与有 ...

  2. [RxJS] Split an RxJS Observable into groups with groupBy

    groupBy() is another RxJS operator to create higher order observables. In this lesson we will learn ...

  3. iOS过场动画调研笔记

    前言 因项目须要,近期一段时间都在调研iOS的过场动画.对于我来说这是一个之前没有太涉及的领域,所以有必要把调研的过程和自己的一些理解纪录下来 为什么要自己定义过场动画? 假设大家有关注Materia ...

  4. jquery 点击其他地方

    <script type="text/javascript"> function stopPropagation(e) { if (e.stopPropagation) ...

  5. [Angular Form] ngModel and ngModelChange

    When using Radio button for Tamplate driven form, we want to change to the value change and preform ...

  6. simple java mail

    <dependency> <groupId>org.simplejavamail</groupId> <artifactId>simple-java-m ...

  7. React中的state与props的再理解

    props可以看做是 property 的缩写的复数,可以翻译为属性,类似于HTML 标签的自定义属性.在大多数React教程里讲 state 和 props 主要的区别在于 props 是不可变的, ...

  8. uva 11552 Fewest Flops 线性dp

    // uva 11552 Fewest Flops // // 二维线性dp // // 首先,在该块必须是相同的来信.首先记录每块有很多种书 // 称为是counts[i]; // // 订购f[i ...

  9. PCI GXL学习之安装篇

    作者:朱金灿 来源:http://blog.csdn.net/clever101 上周简单学习了PCI gxl的使用和二次开发.说实话gxl没有我想象中那么自动化,我原以为把一个数据处理作业扔给gxl ...

  10. 签署 Centennial Program Addendum,使用 Desktop Bridge 将 Win32 应用转制成 UWP

    原文 签署 Centennial Program Addendum,使用 Desktop Bridge 将 Win32 应用转制成 UWP 能上架 Windows 应用商店的并不一定必须是 UWP 应 ...