当覆盖 WPF 模板时,我是否必须单独覆盖每个主题的模板?

发布于 2024-08-26 17:07:48 字数 793 浏览 3 评论 0原文

我需要更改 WPF ComboBox 模板的一小部分。

如果我复制 Luna 主题的现有模板并进行更改,那么最初一切都会正常工作。但如果用户有不同的主题,我的 ComboBox 会保留其自定义主题(这显然是基于 Luna 的),因此看起来不合适。

有没有办法只覆盖模板的一部分,以便模板的大部分仍然遵循 Windows 主题?

我注意到模板的某些部分定义了一个 mwt 命名空间,其中显式引用了 Luna:

xmlns:mwt="...blah blah...=PresentationFramework.Luna"

也许有某种方法可以让这个 mwt 命名空间引用当前主题而不是特定的 Luna 主题?

或者我是否必须为每个主题提供模板的自定义副本?如果微软创建了一个新主题,我是否需要更新我的模板来支持它,会发生什么?

(我要更改的位仅与组合框关闭时显示的 TextBlock 相关。我不会更改下拉列表或按钮。理想情况下,我只需覆盖 SelectionBoxItemTemplate > 在 ComboBox 上,但这是只读的,因此据我所知,我必须重写整个控件模板才能进行任何更改。)

[相关问题,但没有答案:< a href="https://stackoverflow.com/questions/980055/adjust-a-control-template-and-still-respect-the-theme-of-the-os">调整控制模板并仍然尊重主题操作系统?]

I have a requirement to change a very small part of the WPF ComboBox's template.

If I take a copy of the existing template for the Luna theme and make the change it all works fine initially. But if the user has a different theme, my ComboBox retains it's custom theme, (Which is obviously based on Luna) so looks out of place.

Is there any way of overriding just parts of a template so the majority of the template still respects the windows theme?

I notice that parts of the template define an mwt namespace with an explicit reference to Luna:

xmlns:mwt="...blah blah...=PresentationFramework.Luna"

perhaps there is some way to have this mwt namespace refer to the current theme rather than specifically the Luna one?

Or do I have to provide a custom copy of the template for each theme? And what happens if MS creates a new theme, will I then have to update my template to support it?

(The bit I am changing just relates to the TextBlock displayed when the combo box is closed. I'm not changing the dropdown or the button. Ideally I would just override the SelectionBoxItemTemplate on the ComboBox, but this is readonly, so as far as I can tell I have to override the whole control template to make any changes.)

[Related question, but no answers: Adjust a Control Template and still respect the Theme of the OS?]

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

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

发布评论

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

评论(1

海拔太高太耀眼 2024-09-02 17:07:48

要记住的一件事是,一旦
您为您创建一个 ControlTemplate
控制,你正在更换整个
控制模板。

来自: http://msdn.microsoft.com/en-us/library/ms745683 .aspx

我从中得到的信息是,没有,没有办法替换模板的部分内容。该模板被视为一个单元。但是,可能有一些方法可以自定义模板以允许通过样式进行更改(可以基于其他样式)。

如果微软制作了一个新主题,那么您将必须更新自己的自定义控件模板以实现类似的主题。请记住,您是在替换而不是增强控制模板。

One thing to keep in mind is that once
you create a ControlTemplate for you
control, you are replacing the entire
ControlTemplate.

From: http://msdn.microsoft.com/en-us/library/ms745683.aspx

What I gather from that is no, there is no way to replace portions of a template. The template is considered a single unit. However, there may be ways to customize your template to allow changes via styles (which can be based on other styles).

If MS makes a new theme, then you will have to update your own custom control templates to implement a similar theme. Remember, you're replacing, not enhancing the control template.

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