WPF - How do I default the Visibility of a databound Textblock?
Asked Answered
T

1

23

This Textblock, defined below, shows when the window first loads because it has no Datacontext (and hence the converter code is not run) until an item has been selected from another control e.g. TreeView.

<TextBlock
   Name="tbkDocumentNotFound" 
   Style="{StaticResource StandardText}"
   Margin="4,4,2,0" 
   TextWrapping="Wrap"                                    
   Visibility="{Binding Path=IsDownloaded, Converter={StaticResource docNotFoundVisibilityConverter}, Mode=TwoWay}"
   Text="The document could not be found.">
</TextBlock>

So how do I stop the it from appearing when it has no DataContext?

Thanks.

Tradeswoman answered 11/11, 2009 at 13:32 Comment(0)
S
55

To provide a default value (used when the target of a Binding can't be found) you use the FallbackValue, for example:

Visibility="{Binding Path=IsDownloaded, FallbackValue=Collapsed}"

This should be the case when there is no DataContext.

Sideslip answered 11/11, 2009 at 13:38 Comment(2)
Thanks GraemeF. Although I'll just point out that the just the Member name should be specified i.e. Visibility="{Binding Path=IsDownloaded, FallbackValue=Collapsed}"Tradeswoman
This just saved me a lot of time and energy THANK YOU!Cesarean

© 2022 - 2024 — McMap. All rights reserved.