2012-08-04 69 views
0

对不起,设法确保包含所有信息!通用,可绑定,均匀拉伸布局

我需要的细胞图(VM对应一个视图)几乎像这样一个循环:

******** 
    ********** 
************ 
************** 
************** 
************** 
************** 
************ 
    ********** 
    ******** 

一些并发症: 之前,我们火了,我不知道有多少细胞显示。所有单元必须大小相等。整个视图必须可扩展。

在我的简化世界中,我的窗口创建了20个RowViewModel对象,并为每个构造函数传递它应创建的列数。

我的主要观点:

<Viewbox Stretch="Uniform"> 
    <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window}},Path=Rows}" > 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Rows="{Binding Path=Rows.Count}" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 
</Viewbox> 

而且我RowView:

<Grid> 
    <ItemsControl ItemsSource="{Binding Columns}" > 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Columns="{Binding Columns.Count}"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
    </ItemsControl> 
</Grid> 

我的手机的看法:

<Border > 
    <Viewbox Stretch="Uniform" > 
     <Label FontSize="10" Content="{Binding Notice}" Foreground="White" /> 
    </Viewbox> 
</Border> 

目前的情况是,所有的行都是一样的宽度,使与行更少的细胞有更宽的细胞。如果我告诉每行有20个单元格,每个单元格都是相同的大小,但尽管使用了Horizo​​ntalAlignment设置,但所有单元格都保持一致,大概是因为它附加了空白单元格。我假设我可以在我想要的位置插入空白单元格,但这感觉就像是数据的巧妙结合,使显示正确,我相信您会同意B-A-D。

我已经尝试了数以万计的方法,并认为这是迄今为止最接近的,但我错过了它。你能帮忙吗?

感谢您的耐心等待。

回答

0

那么,也许这是不可能的?我最终向ViewModel添加了一个属性,该属性构建网格并在生成过程中分配相应的行/列,然后将内容控件数据绑定到该网格。

它仍然未解决 - 如何在XAML中做到这一点?

的视图:

<Viewbox Stretch="Uniform"> 
    <ContentControl Content="{Binding PrettyGrid}" /> 
</Viewbox> 

在视图模型:

public Grid PrettyGrid 
{ 
    get 
    { 
     return BuildGrid(data); 
    } 
} 

和BuildGrid片段:

private static Grid BuildGrid(List<objects> cellData) 
{ 
    var localGrid = new Grid(); 

    // ... 

    localGrid.RowDefintions.Add(...); 
    localGrid.ColumnDefinitions.Add(...); 

    cellData.ForEach(cell => 
    { 
     ContentControl ctl = new ContentControl(); 
     ctl.Content = cell; 
     Grid.SetRow(ctl, cell.Row); 
     Grid.SetColumn(ctl, cell.Column); 
     localGrid.Children.Add(ctl); 
    }); 

    return localGrid; 
}