Give TextBlock Default Value if result returns null
Asked Answered
J

4

7

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>
Jarlen answered 17/5, 2013 at 15:19 Comment(0)
E
22

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'}" />
Ennius answered 17/5, 2013 at 15:40 Comment(0)
B
3

You can use the TargetNullValue property directly in a binding.

<TextBox Text='{Binding Path=LastName, TargetNullValue="No name found."}' />
Bate answered 17/5, 2013 at 15:43 Comment(0)
I
1

I would bind the TextBlock to a property of an object that didn't return null; make your property return a default value. It appears that you always want the FontStyle to be Italic, so I would just build that in outside the Triggers.

Inhume answered 17/5, 2013 at 15:23 Comment(0)
S
1

In my applications, I find it more reliable to bind my triggers to the actual object my control is bound to. So, if I am looking at Name in the VM for binding of the actual text, I would bind my data trigger to that as well.

<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 Name}" Value="{x:Null}">
                <Setter Property="FontStyle" Value="Italic"/>
                <Setter Property="Text" Value="No Name Found" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Name}" Value="">
                <Setter Property="FontStyle" Value="Italic"/>
                <Setter Property="Text" Value="No Name Found" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</TextBlock.Style>

Steadfast answered 17/5, 2013 at 15:43 Comment(2)
Also, you can use relative source <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}" Value="">Steadfast
Nice approach! It didn't work for my case (with TextBox) at first, so I had to use <Setter Property="Text" Value="{Binding Whatever}" /> (as suggested in another answer).Noriega

© 2022 - 2024 — McMap. All rights reserved.