WPF ControlTemplate 部分替换
假设我们的 Control
有一个非常“XAML 长”的 ControlTemplate
。 我们希望仅向模板添加 1 个按钮。 MSDN 声称“没有办法仅替换控件可视树的一部分”。 我真的必须复制所有 ControlTemplate 并将我的 Button 添加到其中吗?
或者:是否有可能为特定元素提供某种接口?例如一个 TextBox,这样每个人都可以轻松地对其进行模板化,而不必重复整个 ControlTemplate。
谢谢 !
Suppose we have a very 'XAML long' ControlTemplate
for our Control
.
And we want to add just 1 Button to the Template.
MSDN claims that 'There is no way to replace only part of the visual tree of a control'.
Do I really have to copy all that ControlTemplate and add my Button to it?
Or: Is there any possibility how to provide some kind of an interface to a specific element? For example a TextBox, so that everyone could easily template it, without having to reapeat the whole ControlTemplate.
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 qntmfred 所说,你不能直接这样做。但是,如果您拥有要自定义的控件(听起来就像您所做的那样),那么您可以通过添加合适的自定义属性来实现此目的,例如 CustomButtons 属性(控件的每个用户都可以将自己的按钮添加到该属性) )或 ExtensionContent 属性(该控件的每个用户都可以将其设置为他们想要的任何内容 - 将其保留为空,添加单个控件,或添加一个包含任意数量内容的面板)。您的 ControlTemplate 不会为这些属性分配任何语义,而只是按照赋予它们的方式托管它们。例如,假设您提供了 ExtensionContent 属性。您的 ControlTemplate 可以使用 ContentPresenter 来呈现此内容:
并且您的用户可以在其中放入他们想要的任何内容:
另一种可能是提供应用于控件部分的 Style 属性,以便用户可以设置它们的样式(包括更改 的模板(仅)控件的那部分)而不替换整个样式/模板。这是一种“特定元素的接口”想法,例如提供一个应用于“foo”文本框的 FooBoxStyle 属性。
简而言之,其想法是在基本模板中构建一定程度的“部分可替换性”——无论是使用内容、样式、模板还是这些的组合。 WPF 不提供“部分替换”的一般概念,但您可以提供您自己的部分可替换性的特定概念,前提是您可以预测可能需要哪种部分替换。
As qntmfred has said, you can't do this directly. However, if you own the control that you want to customise (which it sounds like you do), then you can do this by adding suitable customisation properties, such as a CustomButtons property (which each user of the control could add their own buttons to) or an ExtensionContent property (which each user of the control could set to whatever content they wanted -- leave it empty, add a single control, or add a panel with as many things as they wanted on it). Your ControlTemplate would assign no semantics to these properties, but just host them as they were given to it. For example, suppose you provided an ExtensionContent property. Your ControlTemplate could present this using a ContentPresenter:
And your users could put whatever they wanted in it:
Another possibly is to provide Style properties that you apply to parts of your control so that users can style them (including changing the template of that part (only) of the control) without replacing the entire style/template. This is kind of your "interface to a specific element" idea e.g. provide a FooBoxStyle property which gets applied to the "foo" TextBox.
In short, the idea is to build a certain measure of "partial replaceability" into the base template -- whether by using content, styles, templates or a combination of these. WPF doesn't provide a general notion of "partial replacement," but you can provide your own specific notion of partial replaceability provided you can predict what kind of partial replacements may be required.
MSDN 是正确的。必须复制整个 ControlTemplate。
MSDN is correct. Gotta copy the entire ControlTemplate.
其实你可以。这不是最舒服的方法,但具体操作方法如下:克隆 WPF ControlTemplate
Actually you can. It's not the most comfortable method, but here is how you do it:Cloning a WPF ControlTemplate