.net 将二维集合绑定到某个控件
我想绑定一个二维集合-复杂数据类型集合的集合。使控件看起来像(n)个富文本框的垂直列表(列) 每个列表将有相同数量的记录.net 将二维集合绑定到某个控件,.net,wpf,mvvm,.net,Wpf,Mvvm,我想绑定一个二维集合-复杂数据类型集合的集合。使控件看起来像(n)个富文本框的垂直列表(列) 每个列表将有相同数量的记录 一种方法是将数据从viewmodel传递到视图,然后在xaml的代码隐藏中以编程方式创建这些列表。但我不想这样做,有更简单的方法吗?只需使用数据绑定和数据模板: <ItemsControl ItemsSource="{Binding MainCollection}"> <ItemsControl.ItemTemplate> &l
一种方法是将数据从viewmodel传递到视图,然后在xaml的代码隐藏中以编程方式创建这些列表。但我不想这样做,有更简单的方法吗?只需使用数据绑定和数据模板:
<ItemsControl ItemsSource="{Binding MainCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding .}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<RichTextBox Text="{Binding .}"/>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
因此外部ItemsControl
绑定到集合集合集合,并在垂直StackPanel
中呈现。对于该集合中的每个集合,都有一个内部ItemsControl
。每个内部ItemsControl
将其集合中的每个项目显示为水平堆栈面板中的RichTextBox
显然,您需要根据具体场景的需要修复绑定路径并进行必要的调整。
<ListBox ItemsSource="{Binding Data}" Grid.Row="1"
Padding="20" Background="Transparent" BorderBrush="Black">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding}" BorderBrush="Black" BorderThickness="2">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Black" Padding="3"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid Width="300">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="1" Text="{Binding Prop1}" />
<TextBlock Grid.Row="2" Text="{Binding Prop2}" />
<TextBlock Grid.Row="3" Text="{Binding Prop3}" />
<TextBlock Grid.Row="4" Text="{Binding Prop4}" />
<TextBlock Grid.Row="5" Text="{Binding Prop5, StringFormat=d}" />
<TextBlock Grid.Row="6" Text="{Binding Prop6,StringFormat=c}" />
<TextBlock Grid.Row="7" Text="{Binding Prop7,StringFormat=c}" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
应在ViewModel中将数据处理为视图可直接绑定的表单;这就是ViewModel的目的。是的,这就是我想要做的,但是如何做呢?当我的列数不固定时?