0

我试图让下面的 TextBlocks 折叠起来,但是它不起作用:

<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当值为或时,它们仅显示为空白""。a 是否DockPanel自动不渲染未显示的控件?(只有 3 个TextBlocks可以适应 72 像素。)

4

1 回答 1

2

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

如果您希望默认 Visibility 为 Visible,则在样式中设置默认值:

<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>
于 2013-01-04T05:21:11.040 回答