C# ItemsPanelTemplate问题

C# ItemsPanelTemplate问题,c#,wpf,xaml,C#,Wpf,Xaml,我需要使用网格作为面板模板。这没什么大不了的,但我还需要在网格中有其他控件。因此,我的ItemsControl如下所示: <ItemsControl x:Name="itemscontname" ItemsSource="{Binding Fields}" Grid.Column="0" ItemsPanel="{StaticResource gridKey}"> <ItemsControl.ItemTemplate>

我需要使用
网格
作为
面板模板
。这没什么大不了的,但我还需要在
网格中有其他控件。因此,我的ItemsControl如下所示:

<ItemsControl x:Name="itemscontname" ItemsSource="{Binding Fields}" Grid.Column="0"
              ItemsPanel="{StaticResource gridKey}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button .../>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemContainerStyle>
         <Style>
            <Style.Setters>
                <Setter Property="Grid.Row" Value="{Binding RowNumber}" />
                <Setter Property="Grid.Column" Value="{Binding ColumnNumber}" />
            </Style.Setters>
         </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

当我这样做的时候,它会破坏一切。我应该如何解决这个问题?

您通常会在ItemsControl的
模板中添加元素,除了
ItemsPanel
模板之外,您还可以添加这些元素

但是,您不能将这些元素添加到ItemsPanel(由ItemsPresenter管理)中的网格中


...
...
...
...

这是干什么用的?这是将项目添加到模板的地方。如果您想在网格中添加内容,请在更新问题之前或之后添加。这样的话,风格就乱了。我的目标是将按钮放在网格的边缘上,就像棋盘游戏一样。
<ItemsPanelTemplate x:Key="gridKey">
    <Grid Grid.Row="0" Grid.Column="0" >
        <Grid.RowDefinitions>
                ....
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
                ....
        </Grid.ColumnDefinitions>
     </Grid>
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="gridKey">
    <Grid Grid.Row="0" Grid.Column="0" >
        <Grid.RowDefinitions>
                ....
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
                ....
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Row="0" Grid.Column="0" 
                   Text="{Binding SomeTextFromVM}"/>
     </Grid>
</ItemsPanelTemplate>
<ItemsControl ...>
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <Grid>
                ...
                <ItemsPresenter/>
                <TextBlock Grid.Row="0" Grid.Column="0" 
                           Text="{Binding SomeTextFromVM}"/> 
            </Grid>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                ...
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        ...
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemContainerStyle>
        ...
    </ItemsControl.ItemContainerStyle>
</ItemsControl>