WPF - Hyperlink to TextBlock DataTemplate / Value Converter

I have a ListBox showing a list of usernames, emails, departments, etc. There is a DataTemplate element that has several text blocks to display each property. One of these text blocks is a hyperlink wrap to display email addresses, for example:

<TextBlock>
   <Hyperlink NavigateUri="{Binding Email}">
      <TextBlock Text="{Binding Email}" />
   </Hyperlink>   
</TextBlock>

      

This works great, but if the person doesn't have an email address, the TextBlock is not automatically reset. So I wrote a value converter and used it in a style like this (simplified):

string s = (String) value;
if (s == ""){
   return Visibility.Collapsed;
}
return Visibility.Visible;

      

And this is the style using ValueConverter:

<Style x:Key="ResultItemTextBoxStyle">
    <Setter Property="TextBlock.Visibility" Value="{Binding Path=Text, RelativeSource={RelativeSource Self}, 
                                                    Converter={StaticResource StringToVisibilityConverter}}"/>
</Style>

      

And I added a style calling the converter to TextBlock:

<TextBlock Style="{StaticResource ResultItemTextBoxStyle}">
   <Hyperlink NavigateUri="{Binding Email}">
      <TextBlock Text="{Binding Email}" />
   </Hyperlink>   
</TextBlock>

      

After this change, the email address is never displayed. In a value converter, the TextBlock is always empty and returns Visibility.Collapsed. This is similar to how it evaluates the TextBlock text before the Hyperlink loads or whatever ...

Please help me figure this out.

Thank!

+1


source to share


3 answers


The transformer you wrote is comparing Text

TextBlock

to empty, not internal text Hyperlink

. I think you really want to install RelativeSource

for the child Hyperlink

, but I don't think it's possible.

Are you sure the size is TextBlock

not compensated? If you put the style in Hyperlink

instead (or even its inner one TextBlock

), maybe this will still work?



Edit: Hyperlink

doesn't have a property Visibility

, so moving the style to it won't help, unfortunately. The only other option I can think of is to create a custom control that comes from TextBlock

, and contains Hyperlink

and TextBlock

. If you add to it Email

DependencyProperty

, then you can use that to determine if the control should be visible or not, both for the value Hyperlink

and its inner TextBlock

.

There seems to be a better way to handle this, but I can't think of anything at the moment.

+1


source


I would do something like this:

<TextBlock>
   <TextBlock.Style>
      <Style>
         <Setter Property="Visibility" Value="Visible"/>
         <Style.Triggers>
            <DataTrigger Binding="{Binding Email}" Value="">
               <Setter Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </TextBlock.Style>
   <Hyperlink NavigateUri="{Binding Email}">
      <TextBlock Text="{Binding Email}" />
   </Hyperlink>   
</TextBlock>

      



I think that creating a value converter is overkill (there should be no crime).

+2


source


I know this isn't really your question, but why are you using text boxes at all?

Just using

    <Hyperlink NavigateUri="{Binding Email}" Content="{Binding Email}" 
Visibility="{Binding Email, Converter={StaticResource StringToVisibilityConverter}}" />

      

should do what you want it to do, if I'm not mistaken.

update: OK, I'm wrong, WPF Hyperlink is not Silverlight HyperlinkButton and doesn't have Content property. Shame on me.

-1


source







All Articles