- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有用的东西
我需要设置作为 StackPanel 子项的某种类型的控件的样式。我正在使用:
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">...</Style>
</StackPanel.Resources>
<TextBlock ...>
...
</StackPanel>
<StackPanel Orientation="Vertical" Background="LightGray">
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green" />
</Style>
</StackPanel.Resources>
<TextBlock Text="plain and simple in stackpanel, green" />
<ContentControl>
<TextBlock Text="inside ContentControl, still green" />
</ContentControl>
<ContentControl>
<ContentControl.Template>
<ControlTemplate TargetType="{x:Type ContentControl}">
<StackPanel Orientation="Vertical">
<ContentPresenter />
<TextBlock Text="how come this one - placed in the template - is not green?" />
</StackPanel>
</ControlTemplate>
</ContentControl.Template>
<TextBlock Text="inside ContentControl with a template, this one is green as well" />
</ContentControl>
</StackPanel>
最佳答案
WPF 考虑 ControlTemplates
成为边界,并且不会在模板内部应用隐式样式(没有 x:Key
的样式)。
但是这个规则有一个异常(exception):任何继承自 Control
的东西。将应用隐式样式。
所以你可以使用 Label
而不是 TextBlock
,它将应用在 XAML 层次结构中进一步定义的隐式样式,但是因为 TextBlock
继承自 FrameworkElement
而不是 Control
,它不会自动应用隐式样式,您必须手动添加它。
我最常见的解决方法是在 ControlTemplate.Resources
中添加一个隐式样式。即 BasedOn
现有隐式TextBlock
风格
<ControlTemplate.Resources>
<Style TargetType="{x:Type TextBlock}"
BasedOn="{StaticResource {x:Type TextBlock}}" />
<ControlTemplate.Resources>
<Application.Resources>
.放置在这里的样式将适用于您的整个应用程序,无论模板边界如何。不过要小心,因为它会将样式应用于 TextBlocks
其他控件的内部,如按钮或组合框<Application.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green" />
</Style>
</Application.Resources>
Label
而不是 TextBlock
因为它继承自 Control
,因此将应用在 ControlTemplate
之外定义的隐式样式x:Key
并将其用作隐式 TextBlock
的基本样式ControlTemplate
里面的样式.它与顶级解决方案几乎相同,但是它用于具有 x:Key
的基本样式。属性<Style x:Key="BaseTextBlockStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green" />
</Style>
...
<ControlTemplate.Resources>
<Style TargetType="{x:Type TextBlock}"
BasedOn="{StaticResource BaseTextBlockStyle}" />
<ControlTemplate.Resources>
关于WPF ControlTemplate 打破样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14503146/
我知道必须有一个简单的答案,但我找不到。 我有一个名为 HoverButton 的按钮样式.
我有一个 NavigationMenuControl用ObservableCollection .一切正常,但是 Style在我的 HtMenuIcon不触发控制。 Visibility在哪里被正确更
我想在运行时定义一个ControlTemplate。这可能吗?我注意到ControlTemplate类的VisualTree属性。我还注意到它使用FrameworkElementFactory类。但是
使用 d:DataContext 可以轻松地为 DataContext 提供设计时数据但是用 {TemplateBinding} 引用的控件属性呢?或 {RelativeSource Template
ControlTemplate 如何处理数据上下文? 使用以下模板 与控制
假设我们有一个非常“长 XAML”ControlTemplate对于我们的Control . 我们想要只需在模板中添加 1 个按钮 . MSDN 声称“没有办法只替换控件的可视化树的一部分”。 我真的
有用的东西 我需要设置作为 StackPanel 子项的某种类型的控件的样式。我正在使用: ... ... 这很好用!每个 TextBlock
我有一个包含以下代码的 xaml 文件:
我想定义一个 ToggleButton,它在选中时以红色前景(例如)显示,而不是按下按钮的默认外观。 我看到视觉状态作为命名元素在 ToggleButton 的 ControlTemplate 中维护
我有一个 ListView,它使用 DataTemplate 呈现每个项目,每个项目都有一个编辑和删除按钮。这两个按钮是模板化的,因此它们使用 Path 来显示图标,并在鼠标悬停时使用 Storybo
....
我有一个 Telerik Tile 的 ControlTemplate,我正在覆盖如下: 我的用户控件如下所示:
我有一个按钮控制模板。我想制作圆角的按钮。我该怎么做? 我尝试使用 CornerRadius 作为边框中的按钮,但它不起作用。按钮的背景已设置为具有角边框的图像,并且按钮看起来很糟糕,因为我无法为按钮
我在 WPF 中创建了这个用户控件 当我编译这个时,我收到以下错误。 “UserControl1” ControlTemplate Tar
我有一个资源字典,其中包含我的窗口的样式。在这种风格中,我定义了模板,并在那里定义了很多东西。除其他外,我定义了一个 Storyboard来为模板中定义的某些事物制作动画。它看起来像这样:
我目前正在尝试找出如何在其他控件模板中重用模板(如标题所述)。我想做的是制作一堆按钮,它们都略有不同,但有几个相似的功能。它们都共享几个相同的图形元素,并具有处理这些图形元素的相同触发器。我希望做的是
我有一个从 slider 派生的类,它使用自定义控件模板并添加了一些依赖属性。我想根据新属性在模板中触发触发器。 例如,我有一个名为 ThumbIsVisible 的新依赖属性,当它设置为 false
我想根据控件的状态更改/动画自定义按钮控件模板的 Foreground 属性。 在 RC0 之前,我设置了 ContentPresenter 的前景,给它一个 x:Name,并在 VisualStat
我已决定尝试自定义 Menu 和 MenuItem 的默认控件模板的许多方面。在 Visual Studio 中,我选择了菜单,在属性面板中找到了"template",然后选择了“转换为新资源...”
我有一个非常大的 ControlTemplate(200 多行),里面有许多嵌套控件。我需要重用此模板,只需对其中一个嵌套控件(复选框的可见性)进行少量更改。显然,我不想复制粘贴到新的 Control
我是一名优秀的程序员,十分优秀!