简体   繁体   English

如何以编程方式将项目添加到WP7中的PanoramaItem

[英]How to programmatically add items to a PanoramaItem in WP7

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. 我正在开发一个具有动态数量的PanoramaItems的应用程序,现在,当前所有这些都通过使用“ ItemsSource”属性将它们的列表绑定到Panorama来添加。

But the problem comes when I try to add things to the created PanoramaItems. 但是,当我尝试将事物添加到创建的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). 现在,我可以添加一个ListBox,它可以按预期工作,但是我真的想添加一些我可以控制的东西(可能是某种自定义控件)。

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. 现在,我发现了很多地方可以通过编辑MainPage.xaml向我展示如何进行此操作,但是由于我不知道需要多少地方,所以我不能(我认为)这样做。

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). 由于PanoramaItem类没有'Items'属性,而只有'Content'属性(我认为这是我需要的),这一事实使问题更加严重。

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 我想我需要为自定义控件定义一个.xaml / .cs文件,然后以某种方式将其应用于PanoramaItem,但我不确定

I'm going to assume here that you're using a MVVM framework here, so if not, I apologize. 我将在这里假设您在这里使用MVVM框架,因此,如果不能,我深表歉意。

Your correct in thinking that you'll probably want to use a user control for the Pano Items. 正确的想法是您可能要对Pano Items使用用户控件。 Once you've got that going here's the new XAML code: 一旦知道了,这里就是新的XAML代码:

<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. 在控件中,您可以直接绑定到PanoViewModels中包含的对象。

If you are doing MVVM and databinding then Barranger Ridler's answer looks good. 如果您正在执行MVVM和数据绑定,那么Barranger Ridler的答案看起来不错。

If you want to write custom C# code for each child, then you need to put a container (eg a Grid or StackPanel) at the Content of each PanoramaItem - and you can then Add to the Children of that container. 如果要为每个子代编写自定义C#代码,则需要在每个PanoramaItem的Content处放置一个容器(例如Grid或StackPanel)-然后可以将其添加到该子代的容器中。 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. 我没有C#代码,但这是Ironruby代码-它向StackPanel添加了5个TextBlocks,然后将该StackPanel设置为PanoramaItem的内容-移植起来应该非常容易。

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 基于代码http://script.iron7.com/#/Script/Detail?scriptId=ed9e4e216a474432a9e88523b201965d&userLowerCaseName=stuart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM