How to settle textblock visibility as collapsed by default...and toggle based on conditions?

1.6k Views Asked by At

I have a TextBlock - which is collapsed by default and will be visible only when it meet two conditions.

I have the below XAML for same. But it is not working as expected.

Any help would be appreciated. thanks

<TextBlock Text="{Binding Path=CC.Name}" VerticalAlignment="Center"  FontWeight="Bold" Margin="0,0,10,0"  Visibility="Collapsed">
    <TextBlock.Style>
        <Style>
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>                                
                        <Condition Binding="{Binding Path=IsP}" Value="True" />    
                            <Condition Binding="{Binding Path=IsC}" Value="True" />
                        </MultiDataTrigger.Conditions>

                        <Setter Property="TextBlock.Visibility" Value="Visible"/>

                    </MultiDataTrigger>                           
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
3

There are 3 best solutions below

3
On BEST ANSWER

you have to use this setter:

<TextBlock Text="{Binding Path=CC.Name}" VerticalAlignment="Center"  FontWeight="Bold" Margin="0,0,10,0">
    <TextBlock.Style>
        <Style>
           <Setter Property="TextBlock.Visibility" Value="Collapsed"/>
           <Style.Triggers>
               <MultiDataTrigger>
                    ...
               </MultiDataTrigger>                           
           </Style.Triggers>
       </Style>
     </TextBlock.Style>
</TextBlock>
1
On

Oh, it is working as expected, however that probably is not what you expected. The key is dependency property precedence. The "local values" (set in the element tag) override everything a style tries to do. You need to extract such properties to a style setter which has a lower precedence than a style trigger.

0
On

Are you expecting an OR or AND to be applied between the conditions. You are only going to get AND. If you want and OR just add another public property the is the OR of the the two.

Represents a trigger that applies property values or performs actions when the bound data meet a set of conditions. http://msdn.microsoft.com/en-us/library/system.windows.multidatatrigger.aspx