Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 448
empo Avatar asked Nov 11 '09 13:11

empo


1 Answers

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.

like image 101
GraemeF Avatar answered Oct 13 '22 00:10

GraemeF