如何正确地将样式应用于 Content Presenter

2024-05-11

我正在研究有关链接按钮的问题的答案:

https://stackoverflow.com/a/3564706/945 https://stackoverflow.com/a/3564706/945

问题是 TextDecoration Underline 样式仅应用于自动生成的 TextBlock。

<Button Style="{StaticResource LinkButton}">Text</Button> 

“文本”带有下划线

<Button Style="{StaticResource LinkButton}"><TextBlock Text='Text' /></Button> 

“文本”没有下划线

为什么它不适用于内容中的任何 TextBlock?

这是样式的相关部分:

<Style x:Key="LinkButton" 
       TargetType="Button"
       BasedOn="{StaticResource ResourceKey={x:Type Button}}"
       >

    <Setter Property="Width" Value="Auto"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <ContentPresenter Content="{TemplateBinding Content}" 
                                  ContentTemplate="{TemplateBinding  ContentTemplate}"
                                  VerticalAlignment="Center"
                                  >
                    <ContentPresenter.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="TextDecorations" Value="Underline" />
                        </Style>
                    </ContentPresenter.Resources>
                </ContentPresenter>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我相信当您将 Framework 元素放入 ContentControl 中时,不会应用模板。如果您还将 TextBlock 样式声明为 Button 的资源,则它在两种情况下都有效。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
<Window.Resources>
    <Style x:Key="LinkButton"
           BasedOn="{StaticResource ResourceKey={x:Type Button}}"
           TargetType="Button">

        <Setter Property="Width" Value="Auto" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter VerticalAlignment="Center"
                                      Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}" >
                        <ContentPresenter.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="TextDecorations" Value="Underline" />
                            </Style>
                        </ContentPresenter.Resources>
                    </ContentPresenter>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
<Grid x:Name="LayoutRoot">
    <StackPanel>
        <Button Style="{StaticResource LinkButton}">Text</Button>
        <Button Style="{StaticResource LinkButton}">
            <Button.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="TextDecorations" Value="Underline" />
                </Style>
            </Button.Resources>
            <TextBlock Text="Text" />
        </Button>
    </StackPanel>
</Grid>
</Window>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何正确地将样式应用于 Content Presenter 的相关文章

  • 使用 TemplateColumns 将 WPF DataGrid 绑定到 DataTable

    我已经尝试了一切但一无所获 所以我希望有人能给我一个顿悟的时刻 我根本无法获得成功提取数据网格中数据的绑定 我有一个包含 MyDataType 的多列的 DataTable public class MyData string nameDa
  • WPF - 检测鼠标按下一段时间

    检测鼠标按钮何时在特定元素上按住特定时间段的最佳方法是什么 感谢您的提示 我创建了一个附加属性以避免任何代码隐藏 using System using System Windows using System Windows Threadin
  • WPF 元素宿主内存泄漏

    我在 Windows 窗体上使用元素主机时遇到奇怪的内存泄漏 我有一个主窗体 它打开另一个窗体 该窗体上只有 elementhost 控件 此时 它没有 wpf 控件子控件 只能打开 1 个主机表单 每次我打开窗体时 应用程序内存都会增加2
  • 即使在 GC Collect 和 WaitForPendingFinalizers 之后,窗口对象在关闭后也未释放?

    这是一个简单的测试应用程序 可帮助了解 WPF 内存使用情况 我想了解的关键是为什么MainWindow即使在关闭并等待 GC 完成之后 仍然被引用并且它的内存没有被释放 参见下面的代码清单 文本 MainWindow Finalizer
  • 网格内的 ContentPresenter 可见性绑定不起作用?

    我有以下网格
  • 更改鼠标悬停时的矩形背景

    所以我有一个没有背景的矩形 当用户将鼠标悬停在其上时 我想给它一个背景渐变 然后当鼠标离开矩形时删除渐变 请有人发布所需的代码 并告诉我将其放在 cs xaml 文件中的位置吗 Thanks This
  • 窗口关闭后仍在调用方法

    首先我不知道这是不是一个愚蠢的问题 我有这样的场景 首先我有一个主窗口 public MainWindow InitializeComponent dt is a System Windows Threading DispatcherTim
  • 在 WPF 中使用 ReactiveUI 提供长时间运行命令反馈的正确方法

    我有一个 C WPF NET 4 5 应用程序 用户将用它来打开某些文件 然后 应用程序将经历很多动作 读取文件 通过许多插件和解析器传递它 这些文件可能相当大 gt 100MB 因此这可能需要一段时间 我想让用户了解 UI 中发生的情况
  • 如何在C#背后的代码中动态创建数据模板并绑定TreeView分层数据

    我有一个场景 其中树视图动态更改其数据模板和数据绑定定义 我在 XAML 中创建了一个树视图 如下所示
  • 先学Silverlight还是先学WPF?

    看来 Silverlight WPF 是 NET 用户界面开发的长期未来 这很棒 因为我可以看到在客户端和 Web 开发端重用 XAML 技能的优势 但看看 WPF XAML Silverlight 它们似乎是非常庞大的技术 那么从哪里开始
  • Web 客户端和 Expect100Continue

    使用 WebClient C NET 时设置 Expect100Continue 的最佳方法是什么 我有下面的代码 我仍然在标题中看到 100 continue 愚蠢的 apache 仍然抱怨 505 错误 string url http
  • 为 Angular2 中的组件加载多个样式表

    我正在构建一个 angular2 应用程序 当我尝试为同一组件加载多个样式表时 我面临多个问题 这是我正在做的代码 import Component from angular core Component selector my tag t
  • WPF ListView/Gridview 允许用户选择多个项目并将它们分组在一起

    我在 MVVM 应用程序中有一个 WPF ListView GridViwe GridView 绑定到 ViewModel 中的列表 要求是用户应该能够选择网格视图的多行 右键单击它并看到上下文菜单 将这些行组合在一起 选择后 所有这些项目
  • 给图像着色[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在尝试着色System Windows Controls Image 该图像包含透明区域 我只是想用颜色给非透明区域着色 例如 图
  • 如何枚举控件的所有依赖属性?

    我有一些 WPF 控件 例如 文本框 如何枚举该控件的所有依赖属性 如 XAML 编辑器所做的那样 不需要使用反射 恕我直言 这是一个坏主意 因为框架已经为此提供了实用程序类 但它们并不明显找到 以下是基于这篇文章的答案 枚举绑定 http
  • WPF 创建同级窗口并关闭当前窗口

    我需要的是我的窗口类中的这样一个事件处理程序 void someEventHandler object sender RoutedEventArgs e MyNewWindow mnw new MyNewWindow mnw Owner W
  • 解释 System.Diagnostics.CodeAnalysis.SuppressMessage

    我在某些应用程序中有这种代码 来自微软 assembly System Diagnostics CodeAnalysis SuppressMessage Microsoft Naming CA1702 CompoundWordsShould
  • 阻止 WPF 文本框增长

    我花了两个小时研究如何避免在输入长文本时我的 WPF TextBox 控件变长 但我无法做到这一点 即使我读过一些关于它的答案 如下所示 停止 wpf textbox from grow with text https stackoverf
  • 使用 ICollectionView 过滤 ObservableCollection

    I have ObservableCollection绑定到dataGrid现在我想过滤我看到需要使用的呈现数据ICollectionView但我不知道如何添加ICollectionView和我的MVVM图案 我的代码简化如下 public
  • 更改其他页面的主窗口内容

    在 WPF 应用程序的主窗口中 我有一个 Badged 元素 来自材料设计 这是我的代码

随机推荐