我有我想绑定到一个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;}
}
我想我的看法绑定到行集合,其中包含列的集合。
我的列集合包含应该由图像(1 3的可能性)来表示一个枚举。 它还包含应该只显示在一列(静态和只是一些文本信息)消息属性。 它还包含应被显示作为该列的标题页眉字符串。
需要注意的是列数为变量(在头被设置为确认的那一刻,但这种情况将会改变以表示动态数据)。
更新:这是瑞秋实施建议后
<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>
这几乎给了我什么,我现在想。 我只坚持我应该为标题做什么。 任何建议都欢迎。