<Window x:Class="WpfApp68.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp68" WindowState="Maximized"
mc:Ignorable="d" Margin="100" Background="Blue"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="100" Background="Yellow">
<Grid Margin="100">
<StackPanel Margin="100" Background="Red">
<Button Content="{Binding ActualWidth,RelativeSource={RelativeSource self}}" Click="OnIncrementAge"/>
<TextBlock Text="{Binding ActualWidth,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}"/>
<TextBlock Text="{Binding ActualWidth,RelativeSource={RelativeSource FindAncestor,AncestorLevel=1,AncestorType=Grid}}"/>
<TextBlock Text="{Binding ActualWidth,RelativeSource={RelativeSource FindAncestor,AncestorLevel=2,AncestorType=Grid}}"/>
<TextBlock Text="{Binding ActualWidth,RelativeSource={RelativeSource FindAncestor,AncestorType=StackPanel}}"/>
</StackPanel>
</Grid>
</Grid>
</Window>
标签:xmlns,http,AncestorLevel,self,relativesource,2006,com,microsoft,schemas From: https://www.cnblogs.com/Fred1987/p/18149607