1

我想像这样实现自定义 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)));
    }

通用的.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 时,我可以看到这些项目。

这里有什么问题?

4

0 回答 0