当内容为null但边框不断显示时,模板化标签应该是不可见的

时间:2011-10-17 09:28:48

标签: wpf xaml border

这一定非常简单但是星期一早上..我有一个标签并用边框覆盖了模板。当我将statusLabel.Content设置为null时,我希望标签变得不可见,但标签的边框仍然是。当statusLabel.Content为null时,如何摆脱边框?下面是相关的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>

更新:感谢您的答案中的代码,它向我显示我之前尝试让边框消失失败,因为我之前测试过的触发器(未在我的问题代码中列出)检查on Value =“null”而不是Value =“{x:Null}”...... DOH!使用正确的触发器设置标签上的可见性非常有效。

2 个答案:

答案 0 :(得分:1)

在Border和ContentPresenter中:

Visibility="{TemplateBinding Visibility}"

它能解决问题吗?

我怀疑它确实如此,但尝试不能伤害:p

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

答案 1 :(得分:1)

脱离我的头顶,完全未经测试:

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