I have a ListBox showing a list of people's names, emails, departments, etc. There is a DataTemplate that has a few TextBlocks to display each property. One of these TextBlocks is wrapping a Hyperlink to show email addresses like so:
<TextBlock>
<Hyperlink NavigateUri="{Binding Email}">
<TextBlock Text="{Binding Email}" />
</Hyperlink>
</TextBlock>
This works fine, but if the person doesn't have an email address, the TextBlock is not collapsed automatically. So I wrote a value converter and used in the style like so (simplified):
string s = (String) value;
if (s == ""){
return Visibility.Collapsed;
}
return Visibility.Visible;
And this is the Style using the ValueConverter:
<Style x:Key="ResultItemTextBoxStyle">
<Setter Property="TextBlock.Visibility" Value="{Binding Path=Text, RelativeSource={RelativeSource Self},
Converter={StaticResource StringToVisibilityConverter}}"/>
</Style>
And I added the style calling the converter to the TextBlock:
<TextBlock Style="{StaticResource ResultItemTextBoxStyle}">
<Hyperlink NavigateUri="{Binding Email}">
<TextBlock Text="{Binding Email}" />
</Hyperlink>
</TextBlock>
After this change the email address is never shown - In the value converter, the text of the TextBlock is always empty and it returns Visibility.Collapsed. It is like it's evaluating the text of the TextBlock before the Hyperlink get loaded or something...
Please help me figure this one out.
Thanks!