未调用 ItemsControl 派生类上的 WPF OnApplyTemplate
标题基本上已经指出了这一点。我读过与同一问题相关的其他博客和帖子,但所提供的解决方案都不适合我。
这是我的代码的简化:
<!-- CustomItemsControl.xaml -->
<ItemsControl x:Class="myNamespace.CustomItemsControl"
xmlns:local="clr-namespace:myNamespace">
<ItemsControl.Resources>
<Style TargetType="{x:Type local:CustomItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomItemsControl}">
<Grid x:Name="MyItemsHost" Background="{TemplateBinding Background}" IsItemsHost="True"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.Resources>
</ItemsControl>
// CustomItemsControl.xaml.cs
namespace myNamespace
{
public partial class CustomItemsControl : ItemsControl
{
public CustomItemsControl()
{
this.DefaultStyleKey = typeof(CustomItemsControl);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var MyItemsHost = (Grid)base.GetTemplateChild("MyItemsHost");
}
}
}
我做错了什么?
The title basically points it out. I've read other blogs and posts related to the same issue, but none of the provided solutions did work for me.
Here's a simplification of my code:
<!-- CustomItemsControl.xaml -->
<ItemsControl x:Class="myNamespace.CustomItemsControl"
xmlns:local="clr-namespace:myNamespace">
<ItemsControl.Resources>
<Style TargetType="{x:Type local:CustomItemsControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomItemsControl}">
<Grid x:Name="MyItemsHost" Background="{TemplateBinding Background}" IsItemsHost="True"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ItemsControl.Resources>
</ItemsControl>
// CustomItemsControl.xaml.cs
namespace myNamespace
{
public partial class CustomItemsControl : ItemsControl
{
public CustomItemsControl()
{
this.DefaultStyleKey = typeof(CustomItemsControl);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
var MyItemsHost = (Grid)base.GetTemplateChild("MyItemsHost");
}
}
}
What I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我以前没有见过这样的做法,我在 Generic.xaml 中定义了模板,因为这是当您转到“项目”->“添加自定义控件 (WPF)”时 Visual Studio 生成的模板。
您可以通过在构造函数中调用
InitializeComponent();
来使您发布的代码正常工作。文档还说您应该使用
Template.FindName("MyItemsHost",this)
而不是GetTemplateChild
。如果您的控件可能需要除网格之外的不同布局,您可能需要使用 ItemsPresenter 并设置 ItemsPanelTemplate。I haven't seen it done that way before, I define the template in Generic.xaml as that is what Visual Studio generates when you go Project->Add Custom Control (WPF).
You can make the code you posted work by calling
InitializeComponent();
in your constructor.Also the documentation says you should use
Template.FindName("MyItemsHost",this)
instead ofGetTemplateChild
. If your control may need a different layout other than the grid you may want to use an ItemsPresenter and set the ItemsPanelTemplate instead.根据您的自定义控件的派生来源,您可能无法对其调用 InitializeComponent()。例如,ContentControl 不提供InitializeComponent。
如果您检查此线程中,您会看到 OnApplyTemplate 从未被调用的原因是因为您将项目定义为类库,而不是自定义控件库。 Visual Studio 向 AssemblyInfo.cs 添加额外信息,以告诉运行时在哪里可以找到控件的模板。
如果将以下代码添加到 AssemblyInfo.cs 文件中,它应该开始正常运行:)
]
Depending on what your Custom Control derives from, you may not be able to call InitializeComponent() on it. For example, a ContentControl doesn't provide InitializeComponent.
If you check this thread, you'll see that the reason OnApplyTemplate never gets called, is because you defined the project as a Class Library, not a Custom Control Library. Visual Studio adds extra information to AssemblyInfo.cs to tell the runtime where to find the template for your control.
If you add the following code to your AssemblyInfo.cs file, it should start behaving correctly:
)]