Wpf – How to set WPF Hyperlink text via XAML

mvvmwpfxaml

I have a button that has its content (text) set dynamically via a style against a backing property as below.

<Button>
   <Button.Style>
      <Style>
         <Setter Property="Button.Content" Value="Advanced Search" />
         <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsAdvancedSearch}" Value="True">
               <Setter Property="Button.Content" Value="Standard Search" />
            </DataTrigger>
         </Style.Triggers>
      </Style>
   </Button.Style>
</Button>

I need to change this button to display just a hyperlink with the same dynamic text. Like this:

<Button>
   <Button.Template>
      <ControlTemplate>
         <TextBlock>
            <Hyperlink>
               Standard Search
            </Hyperlink>
         </TextBlock>
      </ControlTemplate>
   </Button.Template>
</Button>

Is there a way to set the hyperlink's text (inline or some other tag) dynamically via a style still?

I haven't been able to get access to it via XAML. I got it working with a normal binding on a textblock inside the hyperlink but that is creating a redundant property on the viewmodel really.

Best Answer

You can embed another TextBlock inside your Hyperlink and bind it:

<TextBlock>
    <Hyperlink>
        <TextBlock Text="{Binding LinkText}" />
    </Hyperlink>
</TextBlock>
Related Topic