如何通过XAML设置WPF超链接文本

时间:2012-09-14 03:57:48

标签: wpf xaml mvvm

我有一个按钮,其内容(文本)通过样式动态设置,对照后备属性,如下所示。

<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>

我需要更改此按钮以仅显示具有相同动态文本的超链接。像这样:

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

有没有办法通过样式动态设置超链接的文本(内联或其他标签)?

我无法通过XAML访问它。我得到它在超链接内的文本块上进行常规绑定,但这实际上是在viewmodel上创建了一个冗余属性。

4 个答案:

答案 0 :(得分:7)

您可以在Hyperlink中嵌入另一个TextBlock并绑定它:

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

答案 1 :(得分:4)

解决方案是将样式简单地应用于内部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>

答案 2 :(得分:0)

 <GridViewColumn Header="{x:Static lang:Lang.wName}"  CellTemplate="{StaticResource Template_DebitAccount}" />

And use into skin file 

<DataTemplate x:Key="Template_DebitAccount">
    <Border Padding="3" >
        <TextBlock Text="{Binding wDebitAccountName}" Style="{StaticResource TextStyle_Path}">
            <TextBlock.InputBindings>
                <MouseBinding MouseAction="LeftClick" Command="{Binding DataContext.Base_PopPageCommand , RelativeSource={RelativeSource AncestorType=ListView}}" >
                    <MouseBinding.CommandParameter>
                        <MultiBinding Converter="{StaticResource MultiValueBindingConverter}">
                            <Binding Source="AgentSummary" />
                            <Binding Path="Link_Text"/>
                        </MultiBinding>
                    </MouseBinding.CommandParameter>
                </MouseBinding>
            </TextBlock.InputBindings>
        </TextBlock>
    </Border>
</DataTemplate>

答案 3 :(得分:-2)

鉴于:

<Hyperlink x:Name="uriEmailAddress" Click="Hyperlink_Click"></Hyperlink>

代码:

string e = Properties.Settings.Default.Email;
uriEmailAddress.NavigateUri = new Uri("mailto:" + e);
InlineCollection ic = uriEmailAddress.Inlines;
ic.Add(new Run(e));