当 XAML TextBlock 不包含任何数据时,如何使其折叠? [英] How can you get a XAML TextBlock to collapse when it contains no data?
问题描述
我想告诉 WPF:如果 TextBlock 不包含数据,则不要显示它."
TRY #1 使用简单触发器 会产生错误'Text' 成员无效,因为它没有合格的类型名称.em>":
TRY #1 with a simple trigger produces the error "'Text' member is not valid because it does not have a qualifying type name.":
<StackPanel Margin="10">
<TextBlock Padding="10" Background="Yellow" Text="{Binding MainMessage}">
<TextBlock.Triggers>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</TextBlock.Triggers>
</TextBlock>
</StackPanel>
TRY #2 使用 样式触发器 会产生错误类型样式"不包含公共类型转换器类:
TRY #2 with a style trigger produces the error The type 'style' does not contain a public type-converter class:
<UserControl x:Class="TestItemsSource234.Views.SmartForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<Style x:Key="MainMessageStyle" TargetType="TextBlock">
<Style.Triggers>
<Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<StackPanel Margin="10">
<TextBlock Style="MainMessageStyle" Padding="10" Background="Yellow" Text="{Binding MainMessage}"/>
</StackPanel>
</UserControl>
TRY #3 使用 style DataTrigger 会产生相同的错误类型style"不包含公共类型转换器类:
TRY #3 with a style DataTrigger produces the same error The type 'style' does not contain a public type-converter class:
<UserControl x:Class="TestItemsSource234.Views.SmartForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<Style x:Key="MainMessageStyle" TargetType="TextBlock">
<Style.Triggers>
<Trigger>
<DataTrigger Binding="{Binding MainMessage}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<StackPanel Margin="10">
<TextBlock Style="MainMessageStyle" Padding="10" Background="Yellow" Text="{Binding MainMessage}"/>
</StackPanel>
</UserControl>
TRY #4:好吧,这是我的一个愚蠢的疏忽,忘记了 StaticResource,但即便如此,Try #2 和 Try #3 都会出现新错误样式中的 System.Windows.Trigger 类型未知:
TRY #4: OK, that was a dumb oversight of mine, forgot the StaticResource, but even then both Try #2 and Try #3 get a new error The type System.Windows.Trigger in Style is unknown:
<UserControl x:Class="TestItemsSource234.Views.SmartForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<Style x:Key="MainMessageStyle" TargetType="TextBlock">
<Style.Triggers>
<Trigger>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" Value="Collapsed"/>
</Trigger>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<StackPanel Margin="10">
<TextBlock Style="{StaticResource MainMessageStyle}" Padding="10" Background="Yellow" Text="{Binding MainMessage}"/>
</StackPanel>
</UserControl>
那么我该怎么做呢?
好的,这是一个令人抓狂的语法搜索,但有一个快乐的结局,这是有效的版本,希望它对某人有所帮助,吸取了教训:
OK, so that was a maddening syntax hunt with a happy end, here's the version that works, hope it helps somebody, lessons learned:
- 如果触发,则样式
- 如果是样式,则StaticResource
- 如果绑定,则DataTrigger
有效的代码:
<UserControl x:Class="TestItemsSource234.Views.SmartForm"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<UserControl.Resources>
<Style x:Key="MainMessageStyle" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding MainMessage}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<StackPanel Margin="10">
<ItemsControl
ItemsSource="{Binding DataTypeViews}"/>
<TextBlock Style="{StaticResource MainMessageStyle}" Padding="10" Background="Yellow" Text="{Binding MainMessage}"/>
</StackPanel>
</UserControl>
推荐答案
Try #2 或 Try #3 应该没问题 - 问题出在您引用样式的行中 - 您需要使用 'Style="{StaticResource [KeyName]}"' 或 'Style="{DynamicResource [KeyName]}"'.
Either Try #2 or Try #3 should be fine - the problem is in the line where you are referencing the style - you need to use either 'Style="{StaticResource [KeyName]}"' or 'Style="{DynamicResource [KeyName]}"'.
试试这个(在尝试 #2 中):
Try this (in Try #2):
<StackPanel Margin="10">
<TextBlock Style="{StaticResource MainMessageStyle}" Padding="10" Background="Yellow" Text="{Binding MainMessage}"/>
</StackPanel>
在尝试 1 中,您揭示了当前 WPF 版本的限制:元素不直接支持触发器.
In Try 1 you reveal a limitation of current WPF versions: Triggers are not supported directly on elements.
这篇关于当 XAML TextBlock 不包含任何数据时,如何使其折叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!