Reputation: 63
I am developing an application that has a dynamic number of PanoramaItems, now currently these are all added by binging a list of them to the Panorama by using its 'ItemsSource' property.
But the problem comes when I try to add things to the created PanoramaItems. Now, I can add a ListBox, and that works as expected, but I'd really like to add something I have a bit more control over (possibly some type of custom control).
Now, I've found a fair few places that show me how to do this by editing the MainPage.xaml, but since I don't know how many of them I need I cant (I think) do it like that.
The problem is added to by the fact that the PanoramaItem class doesn't have an 'Items' property, just a 'Content' one (which is what I think I need).
I think I need to define a .xaml/.cs file for the custom control and then somehow apply that to the PanoramaItem but I'm really not sure
Upvotes: 5
Views: 4145
Reputation: 66882
If you are doing MVVM and databinding then Barranger Ridler's answer looks good.
If you want to write custom C# code for each child, then you need to put a container (e.g. a Grid or StackPanel) at the Content of each PanoramaItem - and you can then Add to the Children of that container. I don't have c# code to hand, but here's the ironruby code - it adds 5 TextBlocks to a StackPanel then sets that StackPanel as the content of the PanoramaItem - it should be pretty easy to port across.
stack_panel = StackPanel.new
for i in 1..5
t = TextBlock.new
t.font_size = i*24
t.text = "Line " << i.to_s
s.children.add t
end
pi = PanoramaItem.new
pi.header = "Title"
pi.content = stack_panel
panorama.items.add pi
based on code http://script.iron7.com/#/Script/Detail?scriptId=ed9e4e216a474432a9e88523b201965d&userLowerCaseName=stuart
Upvotes: 2
Reputation: 880
I'm going to assume here that you're using a MVVM framework here, so if not, I apologize.
Your correct in thinking that you'll probably want to use a user control for the Pano Items. Once you've got that going here's the new XAML code:
<controls:Panorama ItemsSource="{Binding PanoViewModels}">
<controls:Panorama.ItemTemplate>
<DataTemplate>
<ctl:PanoItemControl />
</DataTemplate>
</controls:Panorama.ItemTemplate>
</controls:Panorama>
obviously this is simplified, but should get the idea across. In your control you can bind directly to objects contained within PanoViewModels.
Upvotes: 3