2012-12-11 103 views
6

我有一个我希望绑定到WPF网格的集合。带有动态列的wpf网格

我面临的问题是列的数量是动态的并且依赖于集合。这里是一个简单的模拟:

public interface IRows 
{ 
    string Message{get;} 
    IColumns[] Columns{get;} 
} 

public interface IColumns 
{ 
    string Header {get;} 
    AcknowledgementState AcknowledgementState{get;} 
} 

public interface IViewModel 
{ 
    ObservableCollection<IRows> Rows {get;} 
} 

我想我的视图绑定到行集合,其中包含一个列的集合。

My Columns集合包含应该由图像表示的枚举(3个可能性中的1个)。它还包含一个Message属性,它应该只显示在一列中(静态的,只是一些文本信息)。它还包含一个Header字符串,该字符串应显示为该列的标题。

The link to what I want to show

注意,列的数目是可变的(在目前的头被设定为确认但是这将改变以表示动态数据)。

更新:这是瑞秋

<ItemsControl 
ItemsSource="{Binding Items, Converter={StaticResource PresentationConverter}}"> 
    <ItemsControl.ItemsPanel> 
    <ItemsPanelTemplate> 
     <Grid ShowGridLines="true" 
     local:GridHelpers.RowCount="{Binding RowCount}" 
     local:GridHelpers.ColumnCount="{Binding ColumnCount}" /> 
    </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemContainerStyle> 
    <Style> 
     <Setter Property="Grid.Row" Value="{Binding RowIndex}"/> 
     <Setter Property="Grid.Column" Value="{Binding ColumnIndex}"/> 
    </Style> 
    </ItemsControl.ItemContainerStyle> 
    <ItemsControl.ItemTemplate> 
    <DataTemplate> 
     <ContentControl Content="{Binding}"> 
     <ContentControl.Resources> 
      <DataTemplate DataType="{x:Type UI:MessageEntity}"> 
      <TextBox Text="{Binding Message}"></TextBox> 
      </DataTemplate> 
      <DataTemplate DataType="{x:Type UI:StateEntity}"> 
      <TextBox Text="{Binding State}"></TextBox> 
      </DataTemplate> 
     </ContentControl.Resources> 
     </ContentControl> 
    </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

实施建议之后,这几乎给了我什么,我现在想。我只是坚持我应该为标题做些什么。 欢迎任何建议。

+0

得到帮助,给予最大的信息的例子提出了一个问题。包含链接... – Harry

+0

简化并添加了一个链接。 – zman

+0

关于如何动态添加行和颜色的链接 - http://stackoverflow.com/questions/13344788/how-to-create-listview-to-a-grid-programmatically/ – Sai

回答

5

您可以使用嵌套ItemsControls这个

这里有一个基本的例子:

<!-- Bind Rows using the default StackPanel for the ItemsPanel --> 
<ItemsControl ItemsSource="{Binding Rows}"> 
    <!-- Set the Template for each row to a TextBlock and another ItemsControl --> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <!-- Need to set Width of name TextBlock so items line up correctly --> 
       <TextBlock Width="200" Text="{Binding Name}" /> 

       <ItemsControl ItemsSource="{Binding Columns}"> 
        <!-- Use a horizontal StackPanel to display columns --> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <StackPanel Orientation="Horizontal" /> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
       </ItemsControl> 
      </StackPanel> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
+0

这几乎是我想要的。我尝试过使用这个想法来实现一个解决方案,但是当我添加一个独立于ItemsControl(仅仅是它上面的堆栈面板)的标题行时,我发现了问题。安排列的宽度以便它们同步是非常棘手的。 – zman

+1

@ManuelZanin您可以使用带有[SharedSizeScope](http://msdn.microsoft.com/zh-cn/library/system.windows.controls.grid.issharedsizescope.aspx)附加属性的网格,但这需要您提前定义列定义。如果列的数量可以更改,我有一些附加属性[在我的博客上](http://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/)您可以绑定列计数而不是手动指定ColumnDefinitions。 – Rachel

+0

好吧,我几乎得到它的工作感谢您的建议。我更新了我的问题。剩下的唯一问题是如何很好地处理标题。如果我不能使用ItemsControl使用它,我可能不得不使用ListView(尽管我对此控件不是很熟悉)。 – zman

2

使用网格方法可能使事情变得比他们应该更复杂。您是否尝试过更改listview的模板,或者为此使用DataGrid?

举一个例子,看看这个项目:http://www.codeproject.com/Articles/25058/ListView-Layout-Manager

或者这一个:http://www.codeproject.com/Articles/16009/A-Much-Easier-to-Use-ListView

如果用网格去,我相信你会不得不增加大量的代码背后管理列和行的数量,它们的大小,单元格内容...而ListView/DataGrid可以让你通过模板动态地做到这一点。

+0

这些链接不处理我看到的列集合。 – Paparazzi

+0

@Blam他们也不处理网格!这正是我的观点:ListView中的DataTemplate可能是OP方案更合适的解决方案。当我启动WPF时,我曾经开始编写一个类,它会生成一个包含未知数量的列+标题的Grid,并最终了解了ListView以及它如何简化这种问题 - 特别是因为OP希望这是一个纯粹的XAML解决方案。 – Joe

+0

你说得好,也许我让自己生活太困难了。我曾考虑过使用ListView/GridView,但我还没有找到任何使用它们来实现我想要的示例(我在WPF上生锈并不起作用)。你发布的例子是有用的,但我认为这对我的目的来说是过分的。 – zman