如何在DataTrigger中切换TextBlock的可见性?

25

这段代码有效(当ControlType="dropDown"时,背景色黄色):

<Window x:Class="TestCollapsed.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:TestCollapsed.Commands"
    Title="Main Window" Height="400" Width="800">
    <Window.Resources>
        <Style x:Key="DropDownStyle" TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ControlType}" Value="dropDown">
                    <Setter Property="Background" Value="Yellow"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <StackPanel>
        <TextBlock Visibility="Visible" 
                   Text="This is going to be the dropdown control."
                   Style="{StaticResource DropDownStyle}"/>
    </StackPanel>
</Window>

但是这段代码无法正常工作(当ControlType="dropDown"时,TextBlock仍然不可见):

<Window x:Class="TestCollapsed.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:TestCollapsed.Commands"
    Title="Main Window" Height="400" Width="800">
    <Window.Resources>
        <Style x:Key="DropDownStyle" TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ControlType}" Value="dropDown">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <StackPanel>
        <TextBlock Visibility="Collapsed" 
                   Text="This is going to be the dropdown control."
                   Style="{StaticResource DropDownStyle}"/>
    </StackPanel>
</Window>

为什么我不能像设置背景一样在样式中设置可见性?

2个回答

57

您正在设置TextBlock的可见性,然后尝试使用样式覆盖它。这是不起作用的。请尝试以下方法:

<Window x:Class="TestCollapsed.Views.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c="clr-namespace:TestCollapsed.Commands"
    Title="Main Window" Height="400" Width="800">
    <Window.Resources>
        <Style x:Key="DropDownStyle" TargetType="TextBlock">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ControlType}" Value="dropDown">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <StackPanel>
        <TextBlock Text="This is going to be the dropdown control."
                   Style="{StaticResource DropDownStyle}"/>
    </StackPanel>
</Window>

13
在这个领悟之前,我不得不犯这个错误十次。 - Anderson Imes
1
是的,我也是。现在这是我在审查触发器代码时首先寻找的东西。 - Bryan Anderson
你好,我想知道如何将 ControlType 绑定到代码后面,从这个 Binding="{Binding ControlType}" ? - Ashley Hooithin

0
我有同样的问题。@Bryan的答案完美无缺! 错误版本如下:
<TextBlock Text="1999-09-09 16:08" VerticalAlignment="Top" Visibility="Collapsed">
                                <TextBlock.Style>
                                    <Style BasedOn="{StaticResource TipTextYellow}" TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding ElementName=Alcohol,Path=IsFocused}"  Value="True">
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>

正确的版本:

<TextBlock Text="1999-09-09 16:08" VerticalAlignment="Top">
                                <TextBlock.Style>
                                    <Style BasedOn="{StaticResource TipTextYellow}" TargetType="TextBlock">
                                        <Setter Property="Visibility" Value="Collapsed"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding ElementName=Alcohol,Path=IsFocused}"  Value="True">
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>

那并没有帮助。 - Sebastian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接