2011-03-27 59 views
6

我有一個類:WPF數據綁定:如何將ItemsCollection中的項目綁定到Grid.Row和Grid.Column?

public class TempClass 
{ 
    public int Row 
    { 
     get { return row; } 
     set { row = value; } 
    } 
    public int Column 
    { 
     get { return column; } 
     set { column = value; } 
    } 
    public string Value 
    { 
     get { return this.value; } 
     set { this.value = value; } 
    } 

    int row; 
    int column; 
    string value; 

    public TempClass(int row, int column, string value) 
    { 
     this.row = row; 
     this.column = column; 
     this.value = value; 
    } 
} 

我創建了一個List<TempClass> DummyCollection和我把它綁定到一個ItemsControlItemsControlItemsPanel屬性使用UniformGrid面板。這裏是XAML代碼:

<ItemsControl ItemsSource="{Binding Path=DummyCollection}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Rows="3" Columns="5" /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate DataType="TempClass"> 
      <Border> 
       <TextBlock Text="{Binding}" /> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

我希望能夠綁定在ItemsControlTempClass項目到特定的細胞,也就是我想設置的項目容器的Grid.RowGrid.Column性能匹配和Column性質的TempClass項目。我如何實現這一目標?謝謝。

回答

7
<ItemsControl> 
    <ItemsControl.ItemContainerStyle> 
     <Style> 
      <Setter Property="Grid.Row" Value="{Binding Row}"/> 
      <Setter Property="Grid.Column" Value="{Binding Column}"/> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 
    ... 
</ItemsControl> 
+0

謝謝肯特。我注意到,例如,如果我的'List DummyCollection'集合中只有一個項目,並且該項目具有'Row = 2'和'Column = 3',則該項目被放置在'UniformGrid的開頭'。那是因爲網格中的所有先前單元格的寬度和高度都設置爲0? – Boris 2011-03-27 18:58:04