我正在尝试让下面的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像素。)
答案 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>