在ControlTemplate中也放置一个TargetType:
<ControlTemplate TargetType="{x:Type local:WorkSheet}">
更新2:
我在一个辅助项目中复制了你的东西,你有两个问题:
第一个问题是样式中的ItemTemplate setter将触发StackOverflow异常(多么讽刺;))。删除ContentPresenter、删除整个模板或使用键。
第二个问题是
GetContainerForItemOverride
方法。删除此方法将给我的东西在屏幕上!
这是我的密码:
public sealed class WorkSheet : ItemsControl
{
/// <inheritdoc />
protected override bool IsItemItsOwnContainerOverride(object item)
{
return (item is WorkTile);
}
}
<Window x:Class="WpfApplication8.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication8="clr-namespace:WpfApplication8"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="{x:Type WpfApplication8:WorkSheet}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate >
<Border>
<ItemsPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<WpfApplication8:WorkSheet x:Name="sheet" />
</Grid>
</Window>