当重写WPF模板,我必须单独覆盖每个主题的模板? [英] When overriding WPF templates do I have to override each theme's template separately?

查看:189
本文介绍了当重写WPF模板,我必须单独覆盖每个主题的模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有要求改变WPF组合框的模板中的一小部分。

如果我走了月神主题现有模板的副本,使这一切工作正常最初的变化。但是,如果用户都有不同的主题,我的组合框保留它的自定义主题,(这显然是根据月亮),这样看起来不合时宜。

有没有办法覆盖只是部分的模板,所以大部分的模板仍然尊重窗口主题?

我注意到,模板的部件形成MWT命名空间的明确提到月神:

 的xmlns:MWT =...等等等等...... = presentationFramework.Luna
 

也许有一些办法让这个MWT命名空间指的是当前的主题,而不是具体的月亮呢?

还是我必须要为每个主题模板的自定义副本?如果MS创建了一个新的主题发生了什么,我会再有更新我的模板来支持它?

(位我改变只是涉及的TextBlock 时,组合框是关闭的。我不会改变下拉或者按钮显示。理想我只想覆盖 SelectionBoxItemTemplate 组合框,但这是只读的,所以据我可以告诉我有覆盖整个控制模板进行任何更改。)

<子> [相关的问题,但没有答案:<一href="http://stackoverflow.com/questions/980055/adjust-a-control-template-and-still-respect-the-theme-of-the-os">Adjust控制模板,仍然尊重OS的主题?]

解决方案
  

有一点要记住的是,一旦   您创建的ControlTemplate你   控制,要更换整个   控件模板。

从:<一href="http://msdn.microsoft.com/en-us/library/ms745683.aspx">http://msdn.microsoft.com/en-us/library/ms745683.aspx

我从收集的没有,有没有办法更换模板的一部分。该模板被认为是一个单一的单元。但是,有很多方法可以自定义模板,允许通过的风格变化(可根据其他样式)。

如果MS使一个新的主题,那么你将不得不更新自己的自定义控制模板来实现一个相似的主题。记住,你替换,而不是增强了控件模板。

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?]

解决方案

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.

这篇关于当重写WPF模板,我必须单独覆盖每个主题的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆