When overriding WPF templates do I have to override each theme's template separately?

我只是一个虾纸丫 提交于 2019-12-19 08:49:09

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/2515468/when-overriding-wpf-templates-do-i-have-to-override-each-themes-template-separa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!