【问题标题】:How to apply Hyperlink TextDecorations property via style in XAML for WPF如何通过 XAML 中的样式为 WPF 应用超链接 TextDecorations 属性
【发布时间】:2012-02-03 06:00:37
【问题描述】:

我有这个 xaml

<Button.Content>
  <Hyperlink>
    <Hyperlink.TextDecorations>
      <TextDecoration>
        <TextDecoration.Pen>
          <Pen Thickness="0" />
        </TextDecoration.Pen>
      </TextDecoration>
    </Hyperlink.TextDecorations>
    <Run Text="jumped over" />
  </Hyperlink>
</Button.Content>

我需要从样式中应用它。我的风格是这样的

<Style x:Key="Button_Link" TargetType="{x:Type Hyperlink}">
  <Setter Property="TextDecorations" >
    <Setter.Value>
      <TextDecorations>
        <TextDecoration>
          <TextDecoration.Pen>
            <Pen Thickness="0" />
          </TextDecoration.Pen>
        </TextDecoration>
      </TextDecorations>
    </Setter.Value>
  </Setter>
</Style>

这导致我的样式文件出现错误,该文件是用于松散 XAML 的 ResourceDictionary

我正在像这样将样式应用于超链接

Style="{DynamicResource Button_Link}"

【问题讨论】:

  • Argg...“导致错误”?什么错误?
  • 页面没有在浏览器中呈现

标签: c# wpf xaml binding styles


【解决方案1】:

您无需将笔粗细设置为零即可删除下划线,您可以使用以下样式:

<Style x:Key="Button_Link" TargetType="{x:Type Hyperlink}">            
     <Setter Property="TextDecorations" Value="None" />
</Style>

如果这只是一个简化示例,您可以通过将 &lt;TextDecorations&gt; 替换为 &lt;TextDecorationsCollection&gt; 来修复现有样式,如下所示:

<Style x:Key="Button_Link" TargetType="{x:Type Hyperlink}">            
    <Setter Property="TextDecorations">
        <Setter.Value>
            <TextDecorationCollection>
                <TextDecoration>
                    <TextDecoration.Pen>
                        <Pen Thickness="0" />
                    </TextDecoration.Pen>
                </TextDecoration>
            </TextDecorationCollection>
        </Setter.Value>
    </Setter>
</Style>

此外,虽然不需要,但您的样式是静态的,因此可以像

一样应用
Style="{StaticResource Button_Link}"

【讨论】:

    猜你喜欢
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多