当内容为空但边框保持显示时,模板化标签应该不可见
这一定很简单,但现在是星期一早上..我有一个标签并用边框覆盖了模板。当我将 statusLabel.Content 设置为 null 时,我希望标签变得不可见,但标签的边框仍然可见。当 statusLabel.Content 为空时如何消除边框?下面是相关的xaml:
<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}"...DOH!使用正确的触发器设置标签上的可见性效果非常好。
this must be very simple but it's monday morning.. I have a label and overridden the template with a border. when I set statusLabel.Content to null I expect the label to become non-visible but instead the label's border still is. how do I get rid of the border just when statusLabel.Content is null? below is the associated xaml:
<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>
UPDATE: thanks for the code in your answers, it showed me that my previous attempts to get the border to disappear failed because the trigger I tested with earlier (not listed in my question code) checked on Value="null" instead of Value="{x:Null}"... DOH! using the correct trigger with setting the visibility on the Label works perfectly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Border 和 ContentPresenter 中:
它解决了问题吗?
我怀疑它确实如此,但尝试不会有什么坏处 :p
否则,您需要添加一个绑定到标签的 Content 属性的 DataTrigger,当它为 null 时,将标签的可见性设置为 Collapsed 或 Hidden。
由于内部控件的可见性现在绑定到标签的可见性,因此所有内容都应该消失。
In the Border and the ContentPresenter:
Does it solve the problem?
I doubt it does, but trying can't hurt :p
Otherwise, you need to add a DataTrigger bound to the Content property of the label, and when it is null, set the visibility of the label to Collapsed or Hidden.
Since the inner control's visibility is now bound to the visibility of the label, everything should disappear.
从我的脑海中浮现出来,完全未经测试:
Off the top of my head, completely untested: