Thursday, 29 August 2013

Bind "Matrix" to GridView

Bind "Matrix" to GridView

Let's say I have these kinds of model:
public class Item
{
public string Name
{
get;
set;
}
}
public class Model
{
public List<Item> Items
{
get;
set;
}
}
Let's also say that I have a list of these models in the ViewModel
List<Model> Models. I was wondering if it was possible to bind the content
of Items in a GridView automatically relatively to the list Models, but I
can't find anything related on the internet.
I tried putting a GridView within a GridView through the DataTemplate like
this :
<DataTemplate x:Key="Style1">
<Grid HorizontalAlignment="Left" Width="200" Height="200">
<TextBlock Text="{Binding Item.Name}"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="Style2">
<GridView ItemsSource="{Binding Items}"
ItemTemplate="{StaticResource Style1}"/>
</DataTemplate>
But it is kind of sloppy and I'd like to avoid that.
The reason I want to do this is because I want to be able to update the
list Items dynamically (like setting it to null for instance) and to have
the UI reflect these changes. Basically I need to keep the Model's
references, I cannot "extract" the Items list to group all the Item in a
single list.

No comments:

Post a Comment