2013-02-01 42 views
0

下面是我的一個相關的帖子一些背景: How to databind using to a 2d array of objects of different types綁定列表<列表<MyClass>>細胞在網格

所以我有一個:

List<List<FarmyardSpace>> 

我希望這可以表示爲按鈕在網格的單元格中。我不能像在相關文章中那樣使用UniformGrid,因爲我需要定義Grid中單元格的大小。理想情況下,我不想在我的數據中定義Row和Column字段(儘管也許這應該由ViewModel以某種方式處理?有一天剛開始學習這些東西,而且我仍然圍繞着WPF和MVVM) 。

這裏是我的最新嘗試,這是不行的(事實上拋出異常),並在這個特殊的例子中,我依靠現有的FarmyardSpaces列和行屬性:

<ItemsControl ItemsSource="{Binding FarmyardGrid}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <ItemsControl ItemsSource="{Binding}"> 
       <ItemsPanelTemplate> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="15"/> 
          <RowDefinition /> 
          <RowDefinition Height="15"/> 
          <RowDefinition /> 
          <RowDefinition Height="15"/> 
          <RowDefinition /> 
          <RowDefinition Height="15"/> 
         </Grid.RowDefinitions> 
        </Grid> 
       </ItemsPanelTemplate> 
       <ItemsControl.ItemContainerStyle> 
        <Style TargetType="ContentPresenter"> 
         <Setter Property="Grid.Row" Value="{Binding Row}"/> 
        </Style> 
       </ItemsControl.ItemContainerStyle> 
      </ItemsControl> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="15"/> 
        <ColumnDefinition /> 
        <ColumnDefinition Width="15"/> 
        <ColumnDefinition /> 
        <ColumnDefinition Width="15"/> 
        <ColumnDefinition /> 
        <ColumnDefinition Width="15"/> 
        <ColumnDefinition /> 
        <ColumnDefinition Width="15"/> 
        <ColumnDefinition /> 
        <ColumnDefinition Width="15"/> 
       </Grid.ColumnDefinitions> 
      </Grid> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemContainerStyle> 
     <Style TargetType="ContentPresenter"> 
      <Setter Property="Grid.Column" Value="{Binding Column}"/> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 
</ItemsControl> 

我認爲部分這不起作用的原因是由外部ItemsControl包含的Items不是FarmyardSpaces而是List,所以它們沒有要綁定的Column屬性。我也嘗試了內部ItemsControl中的RowDefinitions和ColumnDefinitions。這擺脫了異常,但也不起作用,似乎對我來說是錯誤的,尤其是考慮到在外部ItemsControl中聲明Columns的相關帖子中使用UniformGrid時的解決方案。

無論如何,我會很感激任何幫助搞清楚這一點,在此先感謝!

回答

1

如果您的收藏品的屬性指定了它們在網格中的位置,那顯然是最簡單的,這就是您現在設置的。如果沒有,您可以使用轉換器爲您計算每個項目的索引,然後將其分配爲行/列值 - 並且它可以在兩個維度上都一樣。這裏的基本轉換器:

public class ItemToIndexConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     object item = values.FirstOrDefault(); 
     IList collection = values.OfType<IList>().LastOrDefault(); 

     if (collection == null || item == null) 
      return 0; 

     return collection.IndexOf(item); 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

和你的XAML的修改版本(削減到3×3)獲得當前項目,並從父ItemsControl的完整集合傳遞到轉換器:

<ItemsControl ItemsSource="{Binding FarmyardGrid}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <ItemsControl ItemsSource="{Binding}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="15"/> 
           <RowDefinition /> 
           <RowDefinition Height="15"/> 
          </Grid.RowDefinitions> 
         </Grid> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
       <ItemsControl.ItemContainerStyle> 
        <Style TargetType="ContentPresenter"> 
         <Setter Property="Grid.Row"> 
          <Setter.Value> 
           <MultiBinding> 
            <MultiBinding.Converter> 
             <local:ItemToIndexConverter/> 
            </MultiBinding.Converter> 
            <Binding/> 
            <Binding Path="ItemsSource" 
              RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}"/> 
           </MultiBinding> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </ItemsControl.ItemContainerStyle> 
      </ItemsControl> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="15"/> 
        <ColumnDefinition /> 
        <ColumnDefinition Width="15"/> 
       </Grid.ColumnDefinitions> 
      </Grid> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemContainerStyle> 
     <Style TargetType="ContentPresenter"> 
      <Setter Property="Grid.Column"> 
       <Setter.Value> 
        <MultiBinding> 
         <MultiBinding.Converter> 
          <local:ItemToIndexConverter/> 
         </MultiBinding.Converter> 
         <Binding/> 
         <Binding Path="ItemsSource" 
           RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}"/> 
        </MultiBinding> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 
</ItemsControl> 
+0

好吧,我對於我現在沒有得到這個,感覺不那麼糟糕:)這個解決方案對我來說很有意義,但我認爲將會有一種處理索引的不太複雜的方式,因爲它似乎是想知道佈局的一個非常基本的事情目的。感謝所有的幫助! – StuartMorgan