如何解决WPF(4.8) 可以在 ContentControl 中使用两个 ControlTemplates 吗? 如嵌套模板
如何在“ContentControl”中使用两个“ControlTemplates”?
我知道 OnApplyTemplate 方法可用于所有继承的类,并且我可以找到包含在 ControlTemplate 中的子对象。
并且因为控件中只有一个 ControlTemplate,所以我理解继承的子控件不能嵌套和定义,除非它们覆盖 ControlTemplate。
而且由于ContentPresenter需要定义为DataTemplate,所以希望ControlTemplate即使在继承的最终控件中也能重复定义。
.xaml
<Style targettype="Preview">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="PART_Border">
<!-- I hope this -->
<nestedTemplatePresenter/>
...
</Border>
</ControlTemplate>
</Setter.Value>
</Setter Property>
</Style>
<Style targettype="Widget" BasedOn="{StaticResource {x:Type Preview}">
<!-- I hope nestedTemplate -->
<Setter Property="nestedTemplate">
<Setter.Value>
<ControlTemplate>
<Border x:Name="PART_Drag">
...
</Border>
</ControlTemplate>
</Setter.Value>
</Setter Property>
</Style>
.cs
public class Preview : ContentControl
{
public override void OnApplyTemplate()
{
if (GetTemplateChild("PART_Border") is Border bd)
{
// Can access 'PART_Border' right.
}
}
}
public class Widget : Preview
{
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (GetTemplateChild("PART_Drag") is Border bd)
{
// I hope access 'PART_Drag'
}
}
}
我想尝试各种方式,并从中获得新的经验和知识。
即使内容不好,也希望大家多多包涵。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。