[Html]
<Window x: Class = "TestOfRelativeSource. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" mc: Ignorable = "d" xmlns: d = "http://schemas.microsoft.com/expression/blend/2008" xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" Height = "350" Width = "525">
<Grid x: Name = "g1" Background = "Red" Margin = "10">
<DockPanel x: Name = "d1" Background = "Orange"
Margin = "10">
<Grid x: Name = "g2" Background = "Yellow"
Margin = "10">
<DockPanel x: Name = "d2" Background = "LawnGreen"
Margin = "10">
<TextBox x: Name = "textBox1"
FontSize = "24" Margin = "10"/>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
</Window>
<Window x: Class = "TestOfRelativeSource. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" mc: Ignorable = "d" xmlns: d = "http://schemas.microsoft.com/expression/blend/2008" xmlns: mc = "http://schemas.openxmlformats.org/markup-compatibility/2006" Height = "350" Width = "525">
<Grid x: Name = "g1" Background = "Red" Margin = "10">
<DockPanel x: Name = "d1" Background = "Orange"
Margin = "10">
<Grid x: Name = "g2" Background = "Yellow"
Margin = "10">
<DockPanel x: Name = "d2" Background = "LawnGreen"
Margin = "10">
<TextBox x: Name = "textBox1"
FontSize = "24" Margin = "10"/>
</DockPanel>
</Grid>
</DockPanel>
</Grid>
</Window>
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
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. Navigation;
Using System. Windows. Shapes;
Namespace TestOfRelativeSource
{
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
// RelativeSource rs = new RelativeSource (RelativeSourceMode. FindAncestor );
// Rs. AncestorLevel = 1;
// Rs. AncestorType = typeof (Grid );
// Binding binding = new Binding ("Name ")
//{
// RelativeSource = rs
//};
// This. textBox1.SetBinding (TextBox. TextProperty, binding );
// RelativeSource rs = new RelativeSource ();
// Rs. AncestorLevel = 2;
// Rs. AncestorType = typeof (DockPanel );
// Binding binding = new Binding ("Name ")
//{
// RelativeSource = rs
//};
// This. textBox1.SetBinding (TextBox. TextProperty, binding );
This. textBox1.SetBinding (TextBox. TextProperty, new Binding ("Name ")
{
RelativeSource = new RelativeSource ()
{
Mode = RelativeSourceMode. Self
}
});
}
}
}
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
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. Navigation;
Using System. Windows. Shapes;
Namespace TestOfRelativeSource
{
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
// RelativeSource rs = new RelativeSource (RelativeSourceMode. FindAncestor );
// Rs. AncestorLevel = 1;
// Rs. AncestorType = typeof (Grid );
// Binding binding = new Binding ("Name ")
//{
// RelativeSource = rs
//};
// This. textBox1.SetBinding (TextBox. TextProperty, binding );
// RelativeSource rs = new RelativeSource ();
// Rs. AncestorLevel = 2;
// Rs. AncestorType = typeof (DockPanel );
// Binding binding = new Binding ("Name ")
//{
// RelativeSource = rs
//};
// This. textBox1.SetBinding (TextBox. TextProperty, binding );
This. textBox1.SetBinding (TextBox. TextProperty, new Binding ("Name ")
{
RelativeSource = new RelativeSource ()
{
Mode = RelativeSourceMode. Self
}
});
}
}
}