标题基本上指出了它。我已阅读与同一问题相关的其他博客和帖子,但没有提供的解决方案为我工作。WPF OnApplyTemplate对ItemsControl的派生类不叫
这里是我的代码的简化:
<!-- 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");
}
}
}
我做错了吗?
补充说明:项目类型为“ClassLibrary”。我试图在ResourceDictionary中的“Themes”文件夹中定义一个名为“Generic.xaml”的样式。我也在我的“AssemblyInfo.cs”中使用了“ThemeInfo”属性,但都没有解决问题。 – 0xbadf00d 2011-06-04 17:54:15