How to make a text box Visibility=Hidden with a trigger

32,209

Solution 1

You need to set the Style.TargetType in order for it to recognize the Visibility property:

<TextBlock Grid.Column="2" VerticalAlignment="Center" FontWeight="Bold" Foreground="Red" Padding="5" Text="This order will be sent to accounting for approval">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=AllowedToSubmit}" Value="True">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

Your binding path to AllowedToSubmit probably needs to have ElementName set to the Window's name, as well.

Solution 2

Another option is to bind TextBlock.Visibility directly to the property:

<Window>
    <Window.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisibility" />
    </Window.Resources>
    <TextBlock Visibility="{Binding Path=AllowedToSubmit, Converter={StaticResource BoolToVisibility}}" />
</Window>

If you want it to work like in your sample, where true hides the TextBlock, then you can write your own converter to convert opposite of the built-in BooleanToVisibilityConverter.

Share:
32,209
Russ
Author by

Russ

As a key member of any technology architecture team I do what it takes to accomplish technical and business goals with quality, efficiency, and accountability. I specialize in providing technology leadership that inspires while bringing new ideas, and technologies that result in high performance, high availability enterprise products. My primary goal has always been to improve efficiency and productivity for the company through direct contributions as well as the technical development and mentorship of the team working with me. I bridge the Communication gap with leadership, team leads, project managers, and internal partners to guarantee reliable and regular software delivery. While simultaneously bringing a broad level of expertise in both agile software development and system integration, as well as a big picture approach to development with organizational goals and measurable business results.

Updated on August 16, 2020

Comments

  • Russ
    Russ over 3 years

    I seem to be having a hard time today. All I want to do is make a TextBox hidden of visible based on a bool value databound to the Window its hosted in.

    What I have just won't compile and I don't understand why. Please help.

    <TextBlock Grid.Column="2" Text="This order will be sent to accounting for approval" 
               Foreground="Red" VerticalAlignment="Center" FontWeight="Bold" Padding="5">
        <TextBlock.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=AllowedToSubmit}" Value="True">
                        <Setter Property="Visibility" Value="Hidden" /> 
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    
  • Andy
    Andy about 15 years
    Not necessarily - he might have set a DataContext further up the tree, and AllowedToSubmit is a property on that object.
  • Adrian
    Adrian about 15 years
    Agreed with Andy. If Russ is using MVVM, he probably has a DataContext to resolve the binding.
  • Russ
    Russ about 15 years
    MVVM. I don't need to set the ElementName. Thanks for the tip though. My years of winforms is proving to be pretty worthless in WPF. :)
  • Sergey Aldoukhov
    Sergey Aldoukhov about 15 years
    Looks like a style within control TargetType could be defaulted to the owner, not sure why MS didn't do this... Any ideas?
  • Robert Macnee
    Robert Macnee about 15 years
    Good call about not needing the ElementName, I'll remove that part of the answer. re: Default TargetType, if the Style were sitting as a resource you'd get the same error, since it could be assigned to anything it needs to know what it will be ahead of time.
  • Michi-2142
    Michi-2142 over 9 years
    This is the most common way to set the visibility in WPF using MVVM. +1
  • MwBakker
    MwBakker almost 4 years
    Except if the property field changes during the presentation of the view, it won't change visibility