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

标签 c# wpf xaml datatrigger

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

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

最佳答案

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

关于c# - 如何切换 TextBlock 在 DataTrigger 中的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1422287/

相关文章:

c# - 在 WPF TextBox 中写入时允许输入键换行

c# - 如何在代码中设置绑定(bind)?

c# - 自动完成文本框在执行查询时卡住。一定有更好的办法!

c# - 在 C# 中确认格林斯彭第十定律

c# - 如何将多个 View 的DataContext设置为ViewModel的一个实例

c# - 在 Xamarin.Forms 中存储应用程序数据的最佳 Environment.SpecialFolder 是什么?

c# - 如何让 XAML 中的图像显示为它们的实际大小?

c# - 如何创建返回集合的 XAML 标记扩展

c# - 如何通过匹配字段或属性名称从不同的类复制数据

C#计算小数