C# Binding RelativeSource
<WrapPanel x:Name="WrapPanelName"> <Border BorderThickness="1" BorderBrush="Blue"> <TextBox x:Name="tbFindAncestor" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type WrapPanel}}, Path=Name}" MinWidth="120"/> </Border> </WrapPanel>
注:AncestorLevel指的是以Bingding目标控件为起点的层级偏移量,Border的偏移量是1,WrapPanel的偏移量是2, AncestorType指的是要找的目标对象的类型,AncestorLevel必须参考AncestorType使用,比如设置了AncestorType={x:Type WrapPanel},则Bingding在寻找时会忽略非WrapPanel的控件,此时WrapPanel的偏移量是1,Border不是WrapPanel类型所以被忽略