FocusVisualStyle模板,将属性绑定到父级

Spe*_*evy 0 wpf c#-4.0

我为我的用户控件创建了一个FocusVisualStyle,并成功实现了覆盖.我的问题是我希望使用父级的一些属性,但TemplateBinding似乎不起作用.

Control的简化版本定义如下:

<Style TargetType="{x:Type local:Thought}">
  <Setter Property="FocusVisualStyle" Value="{StaticResource ThoughtFocusStyle}" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:Thought}" >
        <Border BorderThickness="{TemplateBinding BorderThickness}" 
                BorderBrush="{StaticResource ThoughtBorderNormalBrush}">
          <!-- other controls -->
        </Border>
      </ControlTemplate>
    </Setter.Value>
   </Setter>
 </Style>
Run Code Online (Sandbox Code Playgroud)

我的自定义FocusVisualStyle定义如下:

<Style x:Key="ThoughtFocusStyle" TargetType="{x:Type Control}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Control}">
        <Border BorderThickness="{TemplateBinding BorderThickness}" 
                BorderBrush="{StaticResource ThoughtBorderFocusBrush}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

如果我在ThoughtFocusStyle中硬编码BorderThickness它按预期工作(Tab进入控件),但使用TemplateBinding却没有.我玩过RelativeSource,但似乎无法正确使用语法(对WPF来说仍然很新).

era*_*zap 5

尝试:

  <Border BorderThickness="{Binding 
                           RelativeSource={RelativeSource Mode=FindAncestor,         
                           AncestorType=Control},
                           Path=BorderThickness}">
Run Code Online (Sandbox Code Playgroud)