2012-04-06 68 views
1

我想用相同的控件填充所有单元格。我现在拥有的是一个控制模板和一个网格。但是我无法在Xaml中找到一种简单的方法将控件添加到所有单元格中。用控件模板填充所有网格单元格

这是代码,我现在所拥有的:

<Window x:Class="AcMidi.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="512" Width="760"> 
    <Window.Resources> 
     <ControlTemplate x:Key="Planet"> 
      <Button Content="Button"></Button> 
     </ControlTemplate> 
    </Window.Resources> 
    <Grid Height="209" Name="grid1" Width="500"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
    </Grid> 
</Window> 

回答

1

你可以使用一个ItemsControl

  • 绑定ItemsSource到有行现在的位置和列位置属性的项目的集合,你可以使用两个for循环轻松创建这样的集合。
  • 充分利用ItemsPanelGrid
  • ItemContainerStyle可以绑定Grid.ColumnGrid.Row对您的项目的属性。
  • ItemTemplate设置为您的模板(因为您不是模板控件,所以它应该是DataTemplate)。
相关问题