Wpf 具有多个datatemplates/ContentPresenter的自定义控件

Wpf 具有多个datatemplates/ContentPresenter的自定义控件,wpf,xaml,Wpf,Xaml,在纸牌游戏中,我需要一个控件,显示网格中4个项目(北、东、南和西)的集合。网格为3x3,分别在单元格(0,1)(1,0)(1,2)和(2,1)中显示内容 <bridge:SeatCollection ItemsSource="{Binding Path=Hands}"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPane

在纸牌游戏中,我需要一个控件,显示网格中4个项目(北、东、南和西)的集合。网格为3x3,分别在单元格(0,1)(1,0)(1,2)和(2,1)中显示内容

    <bridge:SeatCollection ItemsSource="{Binding Path=Hands}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <TextBlock Text="{Binding Path=Name}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </bridge:SeatCollection>

但是在代码中,我可以在哪里以及如何将这些内容添加到网格的中间单元格?

我不知道您在GetContainerForItemOverride中做了什么聪明的事情,所以我将忽略这一部分。您的bridge:SeatCollection是从ItemsControl派生的自定义控件,对吗?因此,它将具有在Themes\Generic.xaml中定义的默认样式。更改该样式,使其具有ContentSource设置为MiddleCell(您的DP)的ContentPresenter。大概是这样的:

<Style TargetType="{x:Type bridge:SeatCollection}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type bridge:SeatCollection}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <ContentPresenter ContentSource="MiddleCell" Grid.Row="1" Grid.Column="1" />

                        <!-- This is for the 'Hands' - Change as needed -->
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


您是否尝试过使用ItemTemplateSelector属性?我意识到我过于强调模板的使用。我只需要将内容添加到网格的中间单元格。我需要中间单元格和手在同一个网格中。GetContainerForItemOverride中的“智能”是为手中的每个项目设置grid.Row和grid.Column。这不再适用于此样式。当我向网格中添加IsItemsHost=“True”时,不允许将ContentPresenter和ItemsPresenter放置在网格中。
    public object MiddleCell
    {
        get { return (object)GetValue(MiddleContentProperty); }
        set { SetValue(MiddleContentProperty, value); }
    }

    public static readonly DependencyProperty MiddleContentProperty =
        DependencyProperty.Register("MiddleCell", typeof(object), typeof(SeatCollection), new PropertyMetadata(0));
<Style TargetType="{x:Type bridge:SeatCollection}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type bridge:SeatCollection}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>

                        <ContentPresenter ContentSource="MiddleCell" Grid.Row="1" Grid.Column="1" />

                        <!-- This is for the 'Hands' - Change as needed -->
                        <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>