WPF relativesource,self,FindAncestor,AncestorType,AncestorLevel,PreviousData,TemplatedParent


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


 

 

 

posted @ 2024-04-21 22:04  FredGrit  阅读(13)  评论(0编辑  收藏  举报