开发者

Binding 2 dimensional Collection to some control

I want to bind a 2 dimensional collection - collection of collection of complex data type. So that the control looks likes (n) vertical lists(columns) of rich text boxes.

Each list will have same number of records.

One way is to pass the data to the view from v开发者_开发问答iewmodel and then programmatically create these lists in the code behind of xaml. But I don't want to do that, is there something simpler?


Just use data binding and data templates:

<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>

So the outer ItemsControl binds to the collection of collections and renders in a vertical StackPanel. For each collection in that collection, there is an inner ItemsControl. Each inner ItemsControl displays every item in its collection as a RichTextBox in a horizontal StackPanel.

Obviously you will need to fix up binding paths as appropriate and tweak as necessary for your specific scenario.


 <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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜