C# 绑定路径中的TemplateBinding

C# 绑定路径中的TemplateBinding,c#,wpf,binding,templatebinding,C#,Wpf,Binding,Templatebinding,我有一个自定义控件,它具有依赖属性头。在Generic.xaml中,该控件有一个样式。我想设置模板某些部分的可见性,使其取决于控件中的标题属性 <Style TargetType="{x:Type CustomControl1}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type CustomC

我有一个
自定义控件
,它具有依赖属性
。在
Generic.xaml
中,该控件有一个样式。我想设置模板某些部分的可见性,使其取决于控件中的
标题
属性

<Style TargetType="{x:Type CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CustomControl1}">
                <Grid MinWidth="400" Focusable="False">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>

                    <TextBlock Text="{TemplateBinding Header}" 
                               Style="{DynamicResource SomeStyle}" 
                               Margin="0,0,80,0"
                               Grid.Row="0" Grid.Column="0">
                        <TextBlock.Visibility>



                            <Binding Path="{TemplateBinding Header}">

                            or alternatively

                            <Binding Path="{Binding Path=Header, RelativeSource={RelativeSource TemplatedParent}}">



                                <Binding.Converter>
                                    <converters:ValueConverterGroup>
                                        <converters:StringNullOrEmptyToBooleanConverter />
                                        <BooleanToVisibilityConverter />
                                    </converters:ValueConverterGroup>
                                </Binding.Converter>
                            </Binding>
                        </TextBlock.Visibility>
                    </TextBlock>

                   ...

或者
...
此行引发一个异常:

<Binding Path="{Binding Path=Header, RelativeSource={RelativeSource TemplatedParent}}">

根据异常消息,Binding(我已经知道)xD的Path属性中不能有绑定


如果没有双重绑定,如何实现这一点?如何在不使用TemplateBinding的情况下访问自定义控件的属性?

Path
绑定的属性不是从属属性,我怀疑您可能希望它是这样的:

<TextBlock.Visibility>
   <Binding Path="Header" RelativeSource="{RelativeSource TemplatedParent}">
          <Binding.Converter>
              <converters:ValueConverterGroup>
                   <converters:StringNullOrEmptyToBooleanConverter />
                    <BooleanToVisibilityConverter />
              </converters:ValueConverterGroup>
          </Binding.Converter>
   </Binding>
</TextBlock.Visibility>