I am wondering if anyone tried to do the following or thought about how to do this.
I have a DataGrid WPFToolkit that is bound to an ObservableCollection of elements. Thus, the DataGrid is displayed with the number of rows in the ObservableCollection and as many columns as I defined for the DataGrid. This is all good. Now I need to provide a different view of the same data, but instead the DataGrid is displayed with the number of cells in the ObservableCollection.
So let's say my ObservableCollection has 100 elements. The initial script showed a DataGrid with 100 rows and 1 column. In a modified scenario, I need to show it with 10 rows and 10 columns, where each cell shows the value that was in the original view. In other words, I need to convert a 1D ObservableCollection to a 2D ObservableCollection and map it to a DataGrid. I know how to do this programmatically in code behind, but can this be done in XAML?
Let me simplify the problem a bit if someone might have a crack. The following is XAML:
* Defines an XmlDataProvider just for dummy data
* Creates a DataGrid with 10 columns
o each column is a DataGridTemplateColumn using the same CellTemplate
* The CellTemplate is a simple TextBlock bound to an XML element
XAML , , DataGrid 5 , 10 , ( ). , , , , , 1, 0-4 5-9. , 12 XML, 1 (, 10 ), 2 2 .
XAML, ?
. !
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:custom="http://schemas.microsoft.com/wpf/2008/toolkit"
mc:Ignorable="d"
x:Name="UserControl"
d:DesignWidth="600" d:DesignHeight="400" >
<UserControl.Resources>
<XmlDataProvider x:Key="InventoryData" XPath="Inventory/Books">
<x:XData>
<Inventory xmlns="">
<Books>
<Book ISBN="0-7356-0562-9" Stock="in" Number="9">
<Title>XML in Action</Title>
<Summary>XML Web Technology</Summary>
</Book>
<Book ISBN="0-7356-1370-2" Stock="in" Number="8">
<Title>Programming Microsoft Windows With C#</Title>
<Summary>C# Programming using the .NET Framework</Summary>
</Book>
<Book ISBN="0-7356-1288-9" Stock="out" Number="7">
<Title>Inside C#</Title>
<Summary>C# Language Programming</Summary>
</Book>
<Book ISBN="0-7356-1377-X" Stock="in" Number="5">
<Title>Introducing Microsoft .NET</Title>
<Summary>Overview of .NET Technology</Summary>
</Book>
<Book ISBN="0-7356-1448-2" Stock="out" Number="4">
<Title>Microsoft C# Language Specifications</Title>
<Summary>The C# language definition</Summary>
</Book>
</Books>
<CDs>
<CD Stock="in" Number="3">
<Title>Classical Collection</Title>
<Summary>Classical Music</Summary>
</CD>
<CD Stock="out" Number="9">
<Title>Jazz Collection</Title>
<Summary>Jazz Music</Summary>
</CD>
</CDs>
</Inventory>
</x:XData>
</XmlDataProvider>
<DataTemplate x:Key="GridCellTemplate">
<TextBlock>
<TextBlock.Text>
<Binding XPath="Title"/>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<custom:DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsSynchronizedWithCurrentItem="True"
Background="{DynamicResource WindowBackgroundBrush}" HeadersVisibility="All" RowDetailsVisibilityMode="Collapsed"
SelectionUnit="CellOrRowHeader" CanUserResizeRows="False" GridLinesVisibility="None" RowHeaderWidth="35" AutoGenerateColumns="False"
CanUserReorderColumns="False" CanUserSortColumns="False">
<custom:DataGrid.Columns>
<custom:DataGridTemplateColumn CellTemplate="{StaticResource GridCellTemplate}" Header="01" />
<custom:DataGridTemplateColumn CellTemplate="{StaticResource GridCellTemplate}" Header="02" />
<custom:DataGridTemplateColumn CellTemplate="{StaticResource GridCellTemplate}" Header="03" />
<custom:DataGridTemplateColumn CellTemplate="{StaticResource GridCellTemplate}" Header="04" />
<custom:DataGridTemplateColumn CellTemplate="{StaticResource GridCellTemplate}" Header="05" />
<custom:DataGridTemplateColumn CellTemplate="{StaticResource GridCellTemplate}" Header="06" />
<custom:DataGridTemplateColumn CellTemplate="{StaticResource GridCellTemplate}" Header="07" />
<custom:DataGridTemplateColumn CellTemplate="{StaticResource GridCellTemplate}" Header="08" />
<custom:DataGridTemplateColumn CellTemplate="{StaticResource GridCellTemplate}" Header="09" />
<custom:DataGridTemplateColumn CellTemplate="{StaticResource GridCellTemplate}" Header="10" />
</custom:DataGrid.Columns>
<custom:DataGrid.ItemsSource>
<Binding Source="{StaticResource InventoryData}" XPath="Book"/>
</custom:DataGrid.ItemsSource>
</custom:DataGrid>
</Grid>