我在 wpf 中创建了两个自定义控件:Control_A
和 Control_B
。它们都定义了一个ColorProperty
。 Control_A
的 ControlTemplate
由 Control_B
实例组成,
<ControlTemplate
TargetType="{x:Type Control_A}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Control_B />
</Border>
</ControlTemplate>
我想要的是将A.Color
(目标)绑定(bind)到B.Color
(源)。如何在 XAML 中实现这一点?
最佳答案
可以通过 TwoWay
绑定(bind)来完成。这种方式有它的缺点,但它有效,
<ControlTemplate
TargetType="{x:Type Control_A}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Control_B
Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Color,
Mode=TwoWay}"/>
</Border>
</ControlTemplate>
关于wpf - 如何绑定(bind)到 ControlTemplate 内的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64116550/