2

I have got myself in a bind. I'm trying to bind to a 3rd level list(basically the hierarchy is Food->Veges->Carrots). So my idea is that when you click a page of food, it brings up different subcategories of food, and for example if you select vegetables, it brings up different vegetables, and say for example you click carrot, it brings up different types of carrots based on your selection...and so on, I've been able to bind to the 2nd hierarchy(veges), but can't get to the third hierarchy based on selection. Your help would be appreciated..This is an idea of my classes:

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

    }
}

Then the Vegetable class is like so:

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

    }
}

The carrot class is similar:

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

    }
}

Now, in the code behind I'm binding to a list of Foods, like so, so that it gets the exact food hierarchy from the first page, NB: Items is a list of food that contains Subparts(Foods->Veges->carrots):

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

And finally, my xaml binding for the 3rd page:

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

I'm trying to bind to the CarrotTypeName of a particular Carrot in a list of a vegetables which is in a list of Food( something like that).When I run the code, the index in the code is selecting based on the Items(list of food), not from the veges. Thanks if you understood my challenge.

7
  • What is the error you are facing? Commented Dec 22, 2015 at 4:18
  • Can you show your complete code? Commented Dec 22, 2015 at 7:01
  • @Bells The error is that the CarrotTypeName property is returning empty in the databinding, because im not able to properly navigate from the second page(the veges page). Remember its bound to the Items list(a list of food) Commented Dec 22, 2015 at 9:56
  • @SatyakiChatterjee These are the major parts involved with the problem, its a large project.. if there is something you need clarification on i'll provide Commented Dec 22, 2015 at 9:58
  • 1
    @DavidEti Seems like your class hierarchy is not quite right to me: you have a Food class that has a list of vegetables in it, but a vegetable is just a type of food. Similarly, your Vegetable class has a list of carrots in it, but again carrots are just one type of vegetable/food. The classes you have now are ensuring that all of your foods are vegetables and every vegetable can only contain carrots (whatever that even means). Maybe you should rethink your objects first. Commented Dec 22, 2015 at 16:28

1 Answer 1

1

The solution was to add an ID property to each of the classes(food, vegetable,carrots). Then in the SelectionChanged event of the Vege.xaml, i did this:

private void VegeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // If selected index is -1 (no selection) do nothing
        if (VegeListBox.SelectedIndex == -1)
            return;
//Make the selected item in the VegeListBox an instance of a Vegetable
       Vegetable selectedVege = (sender as ListBox).SelectedItem as Vegetable;
        // Navigate to the new page
        if (selectedVege != null)
        {
            //Navigate to the Carrot page sending the ID property of the selectedVege as a parameter query
           NavigationService.Navigate(new Uri(string.Format("/Carrot.xaml?parameter={0}", selectedVege.ID), UriKind.Relative));
        }
        // Reset selected index to -1 (no selection)
        VegeListBox.SelectedIndex = -1;
    }

NOTE:In my ViewModel i had created a list of Vegetables(with each vegetable containing a list of Carrots) called VegeItems Then in the Carrot.xaml.cs page, you do this on the onNavigatedTo event:

 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;


        }
    }  

Then in the Carrots.xaml, in the ItemSource of the listbox, i put binding to the Carrot(List of carrots) property of the Vegetable class like so:

<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>   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.