Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatrigger on empty string

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?

like image 325
Giffesnaffen Avatar asked Oct 07 '13 22:10

Giffesnaffen


2 Answers

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>
like image 150
Federico Berasategui Avatar answered Nov 02 '22 03:11

Federico Berasategui


Correct using String.Empty in XAML:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
...
<DataTrigger Binding="{Binding SearchText}" Value="{x:Static sys:String.Empty}">
like image 22
Butsaty Avatar answered Nov 02 '22 04:11

Butsaty