当XAML TextBlock不包含任何数据时,如何使其折叠? [英] How can you get a XAML TextBlock to collapse when it contains no data?
问题描述
我想告诉WPF: 如果TextBlock不包含数据,则不显示它。
尝试带有简单触发器的#1 会产生错误 'Text'成员无效,因为它没有限定的类型名称。:
< 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 ,并带有样式触发器产生错误类型'样式'不包含公共类型转换器类:
< ; 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 和样式的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>
< 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 ,但即使尝试#2和尝试#3都遇到新错误类型为System.Windows.Trigger的样式未知:
< 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}填充= 10 Background = Yellow Text = {Binding MainMessage} />
< / StackPanel>
< / UserControl>
那我该怎么做?
答案:
好的,所以这是一个令人发狂的语法搜寻,结局不错,这是可行的版本,希望对您有所帮助,经验教训:
- 如果触发,则为 style
- if触发,然后 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}填充= 10 Background = Yellow Text = {Binding MainMessage} />
< / StackPanel>
< / UserControl>
尝试尝试#2或尝试#3都可以-问题出在引用样式的行中-您需要使用'Style = {StaticResource [KeyName]}或'Style = {DynamicResource [KeyName]}'。
尝试一下(在尝试#2中):
< StackPanel Margin = 10 >
< TextBlock Style = {StaticResource MainMessageStyle}填充= 10 Background = Yellow Text = {Binding MainMessage} />
< / StackPanel>
在尝试1中,您揭示了当前WPF版本的局限性:元素不直接支持触发器。 / p>
I want to tell WPF: "If TextBlock contains no data, then don't show it."
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 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 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: 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>
So how do I do this?
ANSWER:
OK, so that was a maddening syntax hunt with a happy end, here's the version that works, hope it helps somebody, lessons learned:
- if trigger, then style
- if style, then StaticResource
- if binding, then DataTrigger
code that works:
<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>
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]}"'.
Try this (in Try #2):
<StackPanel Margin="10">
<TextBlock Style="{StaticResource MainMessageStyle}" Padding="10" Background="Yellow" Text="{Binding MainMessage}"/>
</StackPanel>
In Try 1 you reveal a limitation of current WPF versions: Triggers are not supported directly on elements.
这篇关于当XAML TextBlock不包含任何数据时,如何使其折叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!