Databinding + Dynamic Pivot

2019-05-17 01:18发布

问题:

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;     
}

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>

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);
}

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...

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.

Any thoughts?

//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?

回答1:

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; }     
}

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

Mainpivot.ItemsSource = List_Menus;

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