2012-04-06 42 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)。