Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give TextBlock Default Value if result returns null

Hello I am trying to give a default value to a textblock if the results returned are null

Here is what I am trying!

All that returns is the String Format I set!

 <TextBlock x:Name="NameTxtBlock" Grid.Column="0" Margin="0,0,40,0" FontFamily="Segoe UI" FontSize="14" Text="{Binding Name, StringFormat='Item Name: {0}'}"  Padding="2">
    <TextBlock.Style>
        <Style TargetType="TextBlock" >                                            
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=NameTxtBlock, Path=Text}" Value="{x:Null}">
                    <Setter Property="FontStyle" Value="Italic"/>
                    <Setter Property="Text" Value="No Name Found" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=NameTxtBlock, Path=Text}" Value="{x:Static System:String.Empty}">
                    <Setter Property="FontStyle" Value="Italic"/>
                    <Setter Property="Text" Value="No Name Found" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
like image 866
KeyboardFriendly Avatar asked May 17 '13 15:05

KeyboardFriendly


2 Answers

You could use TargetNullValue Property. This will return TargetNullValue without StringFormat if the binding returns Null.

<TextBlock Text="{Binding Name, StringFormat='Item Name: {0}', TargetNullValue='No Name Found'}" />
like image 152
LPL Avatar answered Sep 18 '22 14:09

LPL


You can use the TargetNullValue property directly in a binding.

<TextBox Text='{Binding Path=LastName, TargetNullValue="No name found."}' />
like image 30
Walt Ritscher Avatar answered Sep 17 '22 14:09

Walt Ritscher