将主题应用到控件模板

发布于 2024-11-08 06:08:50 字数 260 浏览 4 评论 0原文

到目前为止,基本上我的问题是我正在将控件模板修改为组合框,以便它看起来像我想要的那样。最重要的是,我们的整个项目都使用 ExpressionDark 主题。问题是,当我在 ComboBox 上设置样式以使其使用修改后的模板时,其 ExpressionDark 样式会被覆盖。

我能想到的唯一解决方案是尝试删除模板中的任何显式着色,但这不起作用。还有 OnApplyTemplate() 但我不确定应该如何使用它。

有人对我如何解决这个问题有一些建议吗?

谢谢

Basically my problem so far is I'm modifying the control template to a ComboBox so that it looks the way I want it to. On top of that our entire project is using the ExpressionDark theme. The problem is that when I set the style on the ComboBox so it uses my modified template its ExpressionDark styling gets overridden.

The only solutions I could think of was to try and take out any explicit coloring in the template but that didn't work. There's also the OnApplyTemplate() but I'm not sure how I should use this.

Does anybody has some advice on how I could go about this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不忘初心 2024-11-15 06:08:50

您需要将自定义样式基于 Expression Dark 主题应用的隐式样式。一个元素只能应用一种隐式样式。最重要的是,如果显式设置 Style 属性,则不会应用隐式 Style。

因此,如果您有 Expression Dark 主题的隐式样式:

<Style TargetType="ComboBox" ... />

以及自定义样式,例如:

<Style x:Key="MyStyle" TargetType="ComboBox" ... />

并像这样使用它:

<ComboBox Style="{StaticResource MyStyle}" ... />

那么您需要将 MyStyle 更改为基于隐式样式,因此它的 Setters 和 Triggers 也适用所以:

<Style x:Key="MyStyle" TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}" ... />

这显然要求可以从定义自定义样式的位置访问隐式样式资源。如果隐式样式位于应用程序资源中,那么您应该很好。

编辑:

由于您使用的是 Silverlight,因此不支持 x:Type 部分。您需要修改 Expression Dark 主题才能使其正常工作。

因此,如果您有 Expression Dark 主题的隐式样式:

<Style TargetType="ComboBox" ... />

您需要将其分成两个样式,如下所示:

<Style x:Key="ComboBoxBaseStyle" TargetType="ComboBox" ... />
<Style TargetType="ComboBox" BasedOn="{StaticResource ComboBoxBaseStyle}" />

然后您的自定义样式需要像这样进行修改:

<Style x:Key="MyStyle" TargetType="ComboBox" BasedOn="{StaticResource ComboBoxBaseStyle}" ... />

隐式样式中不应包含任何 Setter。一切都应该在 ComboBoxBaseStyle 版本中完成。

You would need to base your custom Style on the implicit Style applied by the Expression Dark theme. You can only have one implicit Style applied to an element. On top of that, if you set the Style property explicitly then no implicit Style will be applied.

So if you have the implicit Style for the Expression Dark theme:

<Style TargetType="ComboBox" ... />

And a custom Style like:

<Style x:Key="MyStyle" TargetType="ComboBox" ... />

And use it like so:

<ComboBox Style="{StaticResource MyStyle}" ... />

Then you'd need to change MyStyle to be based on the implicit Style, so it's Setters and Triggers are also applied like so:

<Style x:Key="MyStyle" TargetType="ComboBox" BasedOn="{StaticResource {x:Type ComboBox}}" ... />

This obviously requires that the implicit Style resource be accessible from where you define your custom Style. If the implicit Style is in the application resources, then you should be good.

EDIT:

Since you are using Silverlight, the x:Type part is not supported. You'd need to modify the Expression Dark theme to get this to work.

So if you have the implicit Style for the Expression Dark theme:

<Style TargetType="ComboBox" ... />

You would need to break it out into two Styles like so:

<Style x:Key="ComboBoxBaseStyle" TargetType="ComboBox" ... />
<Style TargetType="ComboBox" BasedOn="{StaticResource ComboBoxBaseStyle}" />

Then your custom Style would need to be modified like so:

<Style x:Key="MyStyle" TargetType="ComboBox" BasedOn="{StaticResource ComboBoxBaseStyle}" ... />

The implicit Style should not have any Setters in it. Everything should be done in the ComboBoxBaseStyle version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文