数据绑定 + 动态枢轴 [英] Databinding + Dynamic Pivot

查看:21
本文介绍了数据绑定 + 动态枢轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 PivotItem 动态添加到模板化和数据绑定的 Pivot 时遇到问题.

I am having troubles with dynamically adding a PivotItem to a templated and databound Pivot.

正在使用的类(稍微简化以使其易于理解);

The classes in use (a bit simplified to keep it quickly comprehensable);

class Menu {
    string Name
    List<Dish> Dishes_1;
    List<Dish> Dishes_2;
    List<Dish> Dishes_3;    
}
class Dish {
    string Description
    string Price;     
}

我想使用枢轴来显示菜单对象列表.我根据该列表中的项目数动态创建 PivotItems.因此,每个 PivotElement 应遵循相同的布局并表现相同.布局模板和数据绑定在 .xaml 中完成,如下所示;

I want to use a Pivot to display a list of Menu-Objects. I create PivotItems dynamically based on the number of items in that list. Each PivotElement should thus follow the same layout and behave the same. The lay-out template and databinding is done in the .xaml as following;

    <phone:Pivot x:Name="Mainpivot">
        <phone:Pivot.HeaderTemplate>
          <DataTemplate>
            <TextBlock Text="{Binding Name}" />
          </DataTemplate>
        </phone:Pivot.HeaderTemplate>    
        <phone:Pivot.ItemTemplate>
           <DataTemplate>
                <ListBox>

                    <TextBlock Text="Dishes_1"/>
                    <ListBox ItemsSource="{Binding Dishes_1}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <TextBlock Text="{Binding Description}"/>
                                    <TextBlock Text="{Binding Price}"/>
                                </Grid>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                   // ...
                   // this is repeated 3 times; 
                   //a textblock and listbox per List<Dishes> in the Menu-class

                </ListBox>
            </DataTemplate>
        </phone:Pivot.ItemTemplate>
    </phone:Pivot>

我使用以下内容填充 de .cs 文件中的 Pivot:

I populate the Pivot in de .cs file with the following:

foreach (Menu m in List_Menus) {
    PivotItem p = new PivotItem();
    p.DataContext = m;
    Mainpivot.Items.Add(p);
}

当我将 DataContext 设置为 Menu-Object 时,DataBinding(通过 xaml)在这里不需要任何更多代码(所以我认为?).

As I set the DataContext as a Menu-Object, the DataBinding (through xaml) should not require any more code here (so I think?).

现在的问题是;它不起作用...

The problem now being; it doesn't work...

通过调试器查看,似乎创建的 PivotItem 的行为与 Mainpivot 中定义的模板不同(或者我认为).查看 Mainpivot 确实表明已添加 PivotItems,但仅此而已,我相信它们只是空的全空 PivotItems.在模拟器中执行时,它只显示一个空的 Pivot.

Through looking with the debugger, It appears that the created PivotItem doesn't behave like the template defined in Mainpivot tells (or so I think). Looking at Mainpivot does show that PivotItems have been added, but that's it, I believe they're just empty all-null PivotItems. When executing in the emulator, It just shows an empty Pivot.

有什么想法吗?

//PS:我不使用 ViewModel,因为我发现它们作为概念非常混乱(作为初学者).我认为这与问题无关?

//PS: I don't use ViewModels, as I find them quite confusing (as a beginner) as concept. I don't think that has anything to do with the problem though?

推荐答案

这里有一些事情.首先,要使绑定生效,您需要使用属性而不是字段.

A few things here. First, for your binding to work you'll need to use properties instead of fields.

public class Menu {
    public string Name {get;set;}
    public List<Dish> Dishes_1 { get; set; }
    public List<Dish> Dishes_2 { get; set; }
    public List<Dish> Dishes_3 { get; set; }   
}

public class Dish {
    public string Description { get; set; }
    public string Price { get; set; }     
}

接下来,无需使用 foreach 循环将项目添加到数据透视中,只需设置项目源即可:

Next, instead of your foreach loop to add the items to the pivot just set the items source:

Mainpivot.ItemsSource = List_Menus;

顺便说一句 - 你真的应该考虑学习 MVVM.值得花时间.

BTW - you really should look into learning MVVM. It is worth the time.

这篇关于数据绑定 + 动态枢轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆