【问题标题】:How can I toggle a TextBlock's visibility in a DataTrigger?如何在 DataTrigger 中切换 TextBlock 的可见性?
【发布时间】:2009-09-14 15:30:32
【问题描述】:

此代码有效(当 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>

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

【问题讨论】:

    标签: c# wpf xaml datatrigger


    【解决方案1】:

    您在 TextBlock 上设置 Visibility,然后尝试用样式覆盖它。那是行不通的。试试这个:

    <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>
    

    【讨论】:

    • 这个错误我必须犯 10 次才能陷入困境。
    • 是的,我也是。现在,这是我在使用触发器审查代码时首先要寻找的东西。
    • 嗨,我可以知道如何将 ControlType 绑定到此 Binding="{Binding ControlType}" 后面的代码吗?
    【解决方案2】:

    我也有同样的问题。 @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>
    

    【讨论】:

    • 这没有帮助。
    猜你喜欢
    • 2015-02-13
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多