Reputation: 588
I've got an ItemsControl that has another ItemsControl in it. which means, for each item in the parent list, i want to show all the items in the child list.
so the Xaml is something like this (I'm neglecting some of the DataTemplate)"
<ItemsControl x:Name="dayPanel" Grid.Column="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<ItemsControl x:Name="dayHours" Grid.Row="1" ItemsSource={Binding HourItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Now in the Code behind i Make a Class that creates a list of days, and each day contains a list of hours. so i get something like this:
public WeekPanel()
{
InitializeComponent();
dayPanel.ItemsSource = new Dayx().CreateDays();
}
So.. the first part, that i define in code, works fine. but the child ItemsControl doesn't get populated. I want to bind it to the property: HourItems how do i do that?
Edit: Okay, the problem was that HourItems property was defined as:
Public List<Hour> HourItem {get; set;}
but it worked when i changed it to:
public List<Hour> HourItems
{
get { return hourItems; }
set { hourItems = value; }
}
what's the difference that made it work?
Upvotes: 1
Views: 3507
Reputation: 8058
<ItemsControl x:Name="dayPanel" Grid.Column="1" ItemsSource={Binding}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<ItemsControl x:Name="dayHours" Grid.Row="1" ItemsSource={Binding HourItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
public WeekPanel()
{
InitializeComponent();
dayPanel.DataContext = new Dayx().CreateDays();
}
Try that code above. Basically I've set the datacontext of your itemscontrol object to your datasource, the ItemsSource then takes the value from itself and the DataContext should pass itself down.
Failing that, have a look in the output window while debugging because that usually tells you if something fails to bind.
Upvotes: 2
Reputation: 49974
In your ItemsControl
, try using a RelativeSource
:
<ItemsControl x:Name="dayHours" Grid.Row="1" ItemsSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ItemsSource }">
Helpful links:
Upvotes: 0