RelativeSource和AncesorType的使用
程序员文章站
2022-06-07 10:52:47
...
如果要绑定到对象上的另一个属性:
{Binding Path=PathToProperty, RelativeSource={RelativeSource Self}}
如果您想获得祖先的属性:
{Binding Path=PathToProperty,
RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}
如果希望获得模板父级上的属性(因此可以在ControlTemplate中执行双向绑定)
{Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}
或者,更短的(这只适用于OneWay绑定):
{TemplateBinding Path=PathToProperty}
<Expander Grid.Row="1">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<Rectangle Height="20" Width="8" Fill="#f39800"/>
<TextBlock Margin="8,0,0,0" Text="业务信息" FontSize="14px" FontFamily="微软雅黑" VerticalAlignment="Center"/>
</StackPanel>
</Expander.Header>
<StackPanel Orientation="Vertical" Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type UserControl}},Converter={StaticResource wc}}">
<local:AppropriationUnemploymentInfoEdit x:Name="applyBussinessUnemploymentInfoEdit" Margin="10,10,10,5"/>
</StackPanel>
</Expander>
上一篇: 鼠标进入和离开控件,控件的背景色改变
推荐阅读