我有一个我希望绑定到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字符串,该字符串应显示为该列的标题。
注意,列的数目是可变的(在目前的头被设定为确认但是这将改变以表示动态数据)。
更新:这是瑞秋
<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>
实施建议之后,这几乎给了我什么,我现在想。我只是坚持我应该为标题做些什么。 欢迎任何建议。
得到帮助,给予最大的信息的例子提出了一个问题。包含链接... – Harry
简化并添加了一个链接。 – zman
关于如何动态添加行和颜色的链接 - http://stackoverflow.com/questions/13344788/how-to-create-listview-to-a-grid-programmatically/ – Sai