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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12417915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:20:42  来源:igfitidea点击:

How do you set WPF Hyperlink text via XAML

wpfxamlmvvm

提问by Adam

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.

我无法通过 XAML 访问它。我让它在超链接内的文本块上使用普通绑定工作,但这确实在视图模型上创建了一个冗余属性。

回答by Matt Hamilton

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

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

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

回答by Adam

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>

回答by ATS

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

回答by user3211406

Given:

鉴于:

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

Code:

代码:

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