Wpf 对子元素中的控件属性作出反应

Wpf 对子元素中的控件属性作出反应,wpf,xaml,triggers,Wpf,Xaml,Triggers,我正在创建一个WPF用户控件,并在其中定义了一个名为“Orientation”的依赖属性。它属于Dock类型,允许我设置值Top、Bottom、Left和Right。UserControl在其XAML中还包含一个Border元素。根据方向属性的值,边界应显示在一侧。以下是我迄今为止定义的触发器: <Trigger Property="Orientation" Value="Bottom"> <Setter Property="BorderThickness" Value

我正在创建一个WPF用户控件,并在其中定义了一个名为“Orientation”的依赖属性。它属于
Dock
类型,允许我设置值Top、Bottom、Left和Right。UserControl在其XAML中还包含一个Border元素。根据方向属性的值,边界应显示在一侧。以下是我迄今为止定义的触发器:

<Trigger Property="Orientation" Value="Bottom">
    <Setter Property="BorderThickness" Value="0,1,0,0"/>
</Trigger>
<Trigger Property="Orientation" Value="Top">
    <Setter Property="BorderThickness" Value="0,0,0,1"/>
</Trigger>
<Trigger Property="Orientation" Value="Left">
    <Setter Property="BorderThickness" Value="0,0,1,0"/>
</Trigger>
<Trigger Property="Orientation" Value="Right">
    <Setter Property="BorderThickness" Value="1,0,0,0"/>
</Trigger>

我现在尝试在边框样式中使用这些触发器,但它们找不到方向属性。然后我尝试在UserControl的样式中使用它,将TargetName设置为我的边框,但不能设置TargetName


正确的XAML代码是什么样子的?

在您的边框样式中,使用DataTrigger而不是Trigger,例如

<DataTrigger Binding="{Binding Orientation, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" Value="Bottom">
    <Setter Property="BorderThickness" Value="0,1,0,0"/>
</DataTrigger>

同样,更新所有触发器