我有一个Custom Control
with 依赖属性Header
。该Generic.xaml
控件有一种样式。我想设置模板某些部分的可见性,以便它依赖于Header
控件的属性。
<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 的 Path 属性内不能有 Binding(我已经知道了)xD
没有双重绑定怎么能做到这一点?如何在不使用 TemplateBinding 的情况下访问自定义控件的属性?