Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set WPF Hyperlink text via XAML

Tags:

mvvm

wpf

xaml

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.

like image 452
Adam Avatar asked Dec 27 '22 17:12

Adam


2 Answers

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

<TextBlock>
    <Hyperlink>
        <TextBlock Text="{Binding LinkText}" />
    </Hyperlink>
</TextBlock>
like image 162
Matt Hamilton Avatar answered Jan 22 '23 19:01

Matt Hamilton


Solution was to simply apply the style to an inner Textblock.

            <Button x:Name="SwitchSearchType">
                <Button.Template>
                    <ControlTemplate>
                        <TextBlock>
                            <Hyperlink>
                                <Hyperlink.Inlines>
                                    <TextBlock>
                                        <TextBlock.Style>
                                            <Style>
                                                <Setter Property="TextBlock.Text" Value="Advanced Search" />
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding Path=IsAdvancedSearch}" Value="True">
                                                        <Setter Property="TextBlock.Text" Value="Standard Search" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </TextBlock.Style>
                                    </TextBlock>
                                </Hyperlink.Inlines>
                            </Hyperlink>
                        </TextBlock>
                    </ControlTemplate>
                </Button.Template>
            </Button>
like image 42
Adam Avatar answered Jan 22 '23 20:01

Adam