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.
Foodclass that has a list of vegetables in it, but a vegetable is just a type of food. Similarly, yourVegetableclass 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.