How can I toggle a TextBlock's visibility in a DataTrigger?

后端 未结 2 1752
囚心锁ツ
囚心锁ツ 2021-02-05 05:47

This code works (when ControlType=\"dropDown\" then the background yellow):



        
相关标签:
2条回答
  • 2021-02-05 05:59

    I have the same problem. @Bryan's answer is perfect! There are the wrong and right versions. The wrong version:

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

    The right version:

    <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>
    
    0 讨论(0)
  • 2021-02-05 06:14

    You're setting the Visibility on the TextBlock and then trying to override it with a style. That won't work. Try this:

    <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>
    
    0 讨论(0)
提交回复
热议问题