Visibility = Collapsed在DockPanel中无法正常工作

时间:2013-01-04 04:46:53

标签: .net wpf vb.net visibility

我正在尝试让下面的TextBlocks变为Collapsed,但它不起作用:

<DockPanel Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" LastChildFill="False" >
<DockPanel.Resources>
    <Style x:Key="ReferenceValues" TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <Trigger Property="Text" Value="">
                <Setter Property="Visibility" Value="Collapsed" />
            </Trigger>
            <Trigger Property="Text" Value="{x:Null}">
                <Setter Property="Visibility" Value="Collapsed" />
            </Trigger>
        </Style.Triggers>
    </Style>
</DockPanel.Resources>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropFullName}" Margin="2,2,2,2"  Visibility="Visible"/>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropTitle}" Margin="2,2,2,2"  Visibility="Visible"/>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropUserName}" Margin="2,2,2,2"  Visibility="Visible"/>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropYear}" Margin="2,2,2,2"  Visibility="Visible"/>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropEmailAddress}" Margin="2,2,2,2"  Visibility="Visible"/>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropStartingPage}" Margin="2,2,2,2"  Visibility="Visible"/>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropEndingPage}" Margin="2,2,2,2"  Visibility="Visible"/>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropStartingDate}" Margin="2,2,2,2"  Visibility="Visible"/>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropEndingDate}" Margin="2,2,2,2"  Visibility="Visible"/>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropLastUpdate}" Margin="2,2,2,2"  Visibility="Visible"/>
<TextBlock Style="{StaticResource ReferenceValues}" FontFamily="Segoe UI Light" FontSize="14" DockPanel.Dock="Top" Text="{Binding PropFileFormat}" Margin="2,2,2,2"  Visibility="Visible"/>
...
</DockPanel>

当值为null""时,它们仅显示为空格。 DockPanel是否自动不呈现未显示的控件? (只有3 TextBlocks可以容纳72像素。)

1 个答案:

答案 0 :(得分:2)

每个TextBlock上的Visibility属性的显式设置将覆盖该样式。删除Visibility="Visible"属性,TextBlocks将根据您的样式按预期呈现。

如果您希望默认的可见性为可见,请在样式中设置默认值:

<Style x:Key="ReferenceValues" TargetType="{x:Type TextBlock}">
    <Setter Property="Visibility" Value="Visible" />
    <Style.Triggers>
        <Trigger Property="Text" Value="">
            <Setter Property="Visibility" Value="Collapsed" />
        </Trigger>
        <Trigger Property="Text" Value="{x:Null}">
            <Setter Property="Visibility" Value="Collapsed" />
        </Trigger>
    </Style.Triggers>
</Style>