我有一个Custom Control
依赖属性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属性中没有绑定(我已经知道了)xD
如果没有双重绑定,怎么办呢?如何在不使用TemplateBinding的情况下访问Custom Control的属性?
答案 0 :(得分:3)
Path
的 Binding
属性不是DependencyProperty,我怀疑你可能希望它是这样的:
<TextBlock.Visibility>
<Binding Path="Header" RelativeSource="{RelativeSource TemplatedParent}">
<Binding.Converter>
<converters:ValueConverterGroup>
<converters:StringNullOrEmptyToBooleanConverter />
<BooleanToVisibilityConverter />
</converters:ValueConverterGroup>
</Binding.Converter>
</Binding>
</TextBlock.Visibility>