Wpf 当内容为空但边框持续显示时,模板标签应不可见
这一定很简单,但现在是星期一早上。。我有一个标签,并用边框覆盖了模板。当我将statusLabel.Content设置为null时,我希望标签不可见,但标签的边框仍然可见。当statusLabel.Content为空时,如何去掉边框?下面是关联的xaml:Wpf 当内容为空但边框持续显示时,模板标签应不可见,wpf,xaml,border,Wpf,Xaml,Border,这一定很简单,但现在是星期一早上。。我有一个标签,并用边框覆盖了模板。当我将statusLabel.Content设置为null时,我希望标签不可见,但标签的边框仍然可见。当statusLabel.Content为空时,如何去掉边框?下面是关联的xaml: <StackPanel Orientation="Horizontal"> <Image Name="statusImage" Stretch="None" Vertic
<StackPanel Orientation="Horizontal">
<Image Name="statusImage"
Stretch="None"
VerticalAlignment="Top"
Margin="20, 20, 0, 0"/>
<Label Name="statusLabel"
Margin="20, 20, 0, 0"
VerticalAlignment="Top"
Background="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:StatusLayer}, Path=StatusTextBackground}"
FontSize ="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:StatusLayer}, Path=FontSize}"
FontStyle="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:StatusLayer}, Path=FontStyle}"
FontWeight="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:StatusLayer}, Path=FontWeight}"
Foreground="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:StatusLayer}, Path=StatusTextForeground}"
FontFamily="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:StatusLayer}, Path=FontFamily}" >
<Label.Template>
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" CornerRadius="5">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Label.Template>
</Label>
</StackPanel>
更新:感谢您回答中的代码,它告诉我,我之前尝试让边界消失的尝试失败了,因为我之前测试的触发器(未在我的问题代码中列出)选中了Value=“null”而不是Value=“{x:null}”。。。啊!使用正确的触发器设置标签上的可见性非常有效。在边框和ContentPresenter中:
Visibility="{TemplateBinding Visibility}"
它能解决问题吗
我怀疑这是真的,但尝试也无妨:p
否则,您需要添加绑定到标签内容属性的DataTrigger,当它为null时,将标签的可见性设置为折叠或隐藏由于内部控件的可见性现在绑定到标签的可见性上,因此所有内容都应该消失。在我的头顶上,完全未经测试:
<Label.Template>
<ControlTemplate TargetType="{x:Type Label}">
<Border Name="LabelBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true" CornerRadius="5">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
<Trigger Property="Content" Value="{x:Null}">
<Setter ElementName="LabelBorder" Property="Visibility" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Label.Template>
谢谢!刚刚测试了这个,它就像预期的那样工作,我不得不添加一个可见性触发器,PS:你的意思是?是的,我也可以这么做=)如果这解决了你的问题,请将它标记为答案。在边框和contentpresenter上用于可见性的模板绑定没有必要吗?显然是因为它们在可视化树中低于标签