数据绑定到xaml中对象列表中对象列表中的对象列表

时间:2015-12-21 22:18:31

标签: c# list xaml

我已陷入困境。我正试图绑定到第3级列表(基本上是层次结构是Food-> Veges-> Carrots)。所以我的想法是,当你点击一页食物时,它会带来不同的食物子类别,例如,如果你选择蔬菜,它会带来不同的蔬菜,比如说你点击胡萝卜,它会带来不同类型的胡萝卜根据您的选择...等等,我已经能够绑定到第二层次(veges),但无法根据选择进入第三层次结构。我的帮助将不胜感激。这是我的课程的一个想法:

public class Food: INotifyPropertyChanged
{
public string FoodName {get;set;}
private  List<Vegetable> _veges = new List<Vegetable>();
public List<Vegetable> Veges
 {
        get
        {
            return _veges;
        }
        set
        {
            if (value != _veges)
            {
                _veges = value;
                NotifyPropertyChanged("Veges");
            }
        }

    }
}

然后蔬菜类是这样的:

    public class Vegetable: INotifyPropertyChanged
{
public string VegeName {get;set;}
private  List<Carrots> _carrot = new List<Carrots>();
public List<Carrots> Carrot
 {
        get
        {
            return _carrot;
        }
        set
        {
            if (value != _carrot)
            {
                _carrot = value;
                NotifyPropertyChanged("Carrot");
            }
        }

    }
}

胡萝卜类很相似:

Public class Carrot: INotifyPropertyChanged
{
public string CarrotTypeName {get;set;}
private  List<CarrotType> _carrottype = new List<CarrotType>();
public List<CarrotType> CarrotT
 {
        get
        {
            return _carrottype;
        }
        set
        {
            if (value != _carrottype)
            {
                _carrottype = value;
                NotifyPropertyChanged("CarrotT");
            }
        }

    }
}

现在,在后面的代码我绑定到Foods列表,就像这样,它从第一页获得确切的食物层次结构,NB:Items是包含子部分的食物列表(Foods-&gt ; Veges-&GT;胡萝卜):

public partial class Subpart : PhoneApplicationPage
{
    Food ourItem;
    public Subpart()
    {
        InitializeComponent();

    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        string selectedIndex = "";
        if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
        {
            int index = int.Parse(selectedIndex);
            ourItem = App.ViewModel.Items[index];
            DataContext = ourItem;
        }
    }
}

最后,我的第三页的xaml绑定:

<Grid x:Name="ContentPanel"
          Grid.Row="1"
          Margin="12,0,12,0">
        <ScrollViewer>
            <ListBox x:Name="FileList"
                     ItemsSource="{Binding Path=Carrot}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Style="{StaticResource PhoneTextLargeStyle}"
                                   x:Name="ContentText"
                                   Text="{Binding CarrotTypeName}"
                                   TextWrapping="Wrap" />
                    </DataTemplate>
                </ListBox.ItemTemplate>

            </ListBox>
        </ScrollViewer>
    </Grid>

我正在尝试绑定到Food列表中的特定Carrot的CarrotTypeName(类似的东西)。当我运行代码时,代码中的索引是基于项目(食物清单),而不是veges。谢谢,如果你了解我的挑战。

1 个答案:

答案 0 :(得分:1)

解决方案是为每个类(食物,蔬菜,胡萝卜)添加ID属性。然后在Vege.xaml的SelectionChanged事件中,我这样做了:

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        string parameter = this.NavigationContext.QueryString["parameter"];
        Vegetable vegeItem = null;
        int VegeId = -1;
        if (int.TryParse(parameter, out VegeId))
        {

            Debug.WriteLine(VegeId);
            vegeItem = App.ViewModel.VegeItems.FirstOrDefault(c => c.ID == VegeId);
            DataContext = vegeItem;


        }
    }  

注意:在我的ViewModel中,我创建了一个名为VegeItems的蔬菜列表(每个蔬菜包含一个胡萝卜列表)然后在Carrot.xaml.cs页面中,您可以在onNavigatedTo事件中执行此操作:

<ListBox x:Name="FileList"
                     ItemsSource="{Binding Path=Carrot}"
                     >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Style="{StaticResource PhoneTextLargeStyle}"
                                   x:Name="ContentText"
                                   Text="{Binding CarrotTypeName}"
                                   TextWrapping="Wrap" />
                    </DataTemplate>
                </ListBox.ItemTemplate>

            </ListBox>   

然后在Carrots.xaml中,在列表框的ItemSource中,我将绑定到了类的Carrot(胡萝卜列表)属性,如下所示:

public class Beginning {

private static volatile boolean itIsRunning = true;
final static CountDownLatch startGate = new CountDownLatch(1);

public static void main(String[] args) {

    Thread fibonnaciThread = new Thread(new Fibonnaci(10));
    Thread timerThread = new Thread(new FibTimer());

    fibonnaciThread.start();
    timerThread.start();

    startGate.countDown();
    while (itIsRunning)
        ;

    timerThread.interrupt();

    System.out.println("Interuppted timer");
}

private static class Fibonnaci implements Runnable {

    private int target;

    public Fibonnaci(int target) {
        this.target = target;
    }

    @Override
    public void run() {
        try {
            startGate.await();

            for (int i = 1; i <= target; i++) {
                System.out.print(fibonacci(i) + "  ");

                Thread.sleep(1000);

            }

            itIsRunning = false;
        } catch (InterruptedException e) {

            Thread.currentThread().interrupt();
        }

    }

    // The actual method that creates the fibonacci
    private int fibonacci(int number) {
        if (number == 1 || number == 2) {
            return 1;
        }

        int num1 = 1, num2 = 1, next = 1;

        for (int i = 3; i <= number; i++) {
            next = num1 + num2;
            num1 = num2;
            num2 = next;

        }
        return next;
    }

}

private static class FibTimer extends Thread {

    public void run() {
        try {
            startGate.await();
            while (!Thread.currentThread().isInterrupted()) {
                Thread.sleep(5000);
                System.out.println("Time so far is " + System.nanoTime() + " seconds");
            }
        } catch (InterruptedException e) {

            Thread.currentThread().interrupt();
        }

    }
}
}