我想实现像这样的自定义ItemsControl
http://www.codeproject.com/Articles/290587/A-WPF-TileView-Control
我有2个课程:
HeatMapView (derived from ItemsControl)
和
HeatMapItem (derived from ContentControl)
在HeatMapView中,我从父
覆盖2个方法static HeatMapView()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HeatMapView), new FrameworkPropertyMetadata(typeof(HeatMapView)));
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is HeatMapItem;
}
protected override DependencyObject GetContainerForItemOverride()
{
return new HeatMapItem();
}
HeatMapItem还有一个静态构造函数
static HeatMapItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(HeatMapItem), new FrameworkPropertyMetadata(typeof(HeatMapItem)));
}
Generic.xaml:
<Style TargetType="{x:Type heatmap:HeatMapView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type heatmap:HeatMapView}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Canvas>
<ItemsPresenter/>
</Canvas>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type heatmap:HeatMapItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type heatmap:HeatMapItem}">
<Grid>
<Rectangle Fill="{TemplateBinding Background}"/>
<ContentPresenter ContentSource="Content" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
MainWindow.xaml:
<Window.Resources>
<Style TargetType="heatMap:HeatMapItem">
<Setter Property="Background" Value="Green"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="heatMap:HeatMapItem">
<Grid>
<Border CornerRadius="5" Grid.Row="0" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter ContentSource="Content"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="heatMap:HeatMapView">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel>
<Button Content="{Binding Key}"/>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<heatMap:HeatMapView ItemsSource="{Binding TestModels}" />
</Grid>
启动应用程序时未显示任何内容。 当我删除ItemSource并手动将HeatMapItems添加到具有背景设置的HeatMapView时,我可以看到这些项目。
这里有什么问题?