当内容为空但边框保持显示时,模板化标签应该不可见

发布于 2024-12-10 11:05:55 字数 2475 浏览 0 评论 0原文

这一定很简单,但现在是星期一早上..我有一个标签并用边框覆盖了模板。当我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剧终人散尽 2024-12-17 11:05:55

在 Border 和 ContentPresenter 中:

Visibility="{TemplateBinding Visibility}"

它解决了问题吗?

我怀疑它确实如此,但尝试不会有什么坏处 :p

否则,您需要添加一个绑定到标签的 Content 属性的 DataTrigger,当它为 null 时,将标签的可见性设置为 Collapsed 或 Hidden。
由于内部控件的可见性现在绑定到标签的可见性,因此所有内容都应该消失。

In the Border and the ContentPresenter:

Visibility="{TemplateBinding Visibility}"

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.

时光沙漏 2024-12-17 11:05:55

从我的脑海中浮现出来,完全未经测试:

<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>

Off the top of my head, completely untested:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文