How can I toggle a TextBlock's visibility in a DataTrigger?
31,993
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>
Related videos on Youtube
Author by
Angry Dan
web/software developer, .NET, C#, WPF, PHP, software trainer, English teacher, have philosophy degree, love languages, run marathons my tweets: http://www.twitter.com/edward_tanguay my runs: http://www.tanguay.info/run my code: http://www.tanguay.info/web my publications: PHP 5.3 training video (8 hours, video2brain) my projects: http://www.tanguay.info
Updated on May 17, 2020Comments
-
Angry Dan almost 4 years
This code works (when ControlType="dropDown" then the background yellow):
<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>
But this code does not work (when ControlType="dropDown" then the TextBlock is still invisible):
<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>
Why can't I set visibility in a style as I can background?
-
Anderson Imes over 14 yearsI had to make this mistake 10 times before it sunk in.
-
Bryan Anderson over 14 yearsYeah, me too. Now it's the first thing I look for when reviewing code with Triggers.
-
Sebastian over 4 yearsThat doesn't help.
-
Ashley Hooithin over 3 yearsHi, may i know how to bind the ControlType to code behind from this Binding="{Binding ControlType}" ?