27

How can a DataTrigger change the visibility of stackpanel, based on a binded string? I have the following Xaml

<StackPanel HorizontalAlignment="Right" 
            Orientation="Horizontal" 
            Grid.Column="1"
            Background="#FF7a7a7a">
    <StackPanel.Style>
        <Style TargetType="{x:Type StackPanel}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding SearchText}" Value="">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    Content....
   </StackPanel>

I Know that SearchText gets updates and binds properly outside the StackPanel

Could somebody point me in the right direction?

4
  • When I bind to a TextBoxText property it works. Do you know what your property is returning? Commented Oct 7, 2013 at 22:58
  • My SearchText property is a String type Commented Oct 7, 2013 at 23:01
  • Yes, but is it returning any value or is it returning null Commented Oct 7, 2013 at 23:03
  • Duplicate of stackoverflow.com/questions/5573864/…? Commented Oct 7, 2013 at 23:40

4 Answers 4

46

This:

<DataTrigger Binding="{Binding SearchText}" Value="">
   <Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>

will work for empty string (""), however it will not work for null.

Add another DataTrigger for the null case:

<DataTrigger Binding="{Binding SearchText}" Value="{x:Null}">
   <Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
Sign up to request clarification or add additional context in comments.

1 Comment

You can combine both triggers into one if you add the following as part of the binding expression TargetNullValue=''
16

Correct using String.Empty in XAML:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
...
<DataTrigger Binding="{Binding SearchText}" Value="{x:Static sys:String.Empty}">

Comments

0

Weird as it might sound, the code below works for me:

<StackPanel Background="#FF7a7a7a">
    <StackPanel.Style>
        <Style TargetType="{x:Type StackPanel}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, ElementName=textBlock}" Value="">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </StackPanel.Style>
    <TextBox x:Name="textBlock" Text="" Width="100" Height="30"></TextBox>
</StackPanel> 

Can you tell the value your Property is sending?

2 Comments

The my string was not initialized having a value of null, therefor it seemed to not work.
Yeah, @HighCore was spot on!
0

Try this

<StackPanel.Style>
                                        <Style TargetType="StackPanel">
                                            <Setter Property="Visibility" Value="Collapsed"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding YourBoundPropertyName}" Value="True">
                                                    <Setter Property="Visibility" Value="Visible"/>

                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </StackPanel.Style>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.