2015-12-21 89 views
2

我已經陷入了束縛。我試圖綁定到一個三級列表(基本上是食物 - > Veges->胡蘿蔔)。所以我的想法是,當你點擊一頁食物時,它會產生不同的食物分類,例如,如果你選擇蔬菜,它會產生不同的蔬菜,並說,例如,你點擊胡蘿蔔,它會帶來不同類型的胡蘿蔔根據你的選擇...等等,我已經能夠綁定到第二層次結構(veges),但不能根據選擇到達第三層次結構。您的幫助將是appreciated..This是我班的一個想法:數據綁定到xaml中的對象列表中的對象列表中的對象列表

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

    } 
} 

現在,在後面的代碼中,我綁定到食品列表,就像這樣,以便它從第一頁獲得確切的食物層次結構。注意:Items是包含子部分(食物 - > Veges->胡蘿蔔):

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第3頁裝訂:

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

我試圖綁定到特定的胡蘿蔔的CarrotTypeName的蔬菜是在列表清單食物(類似的東西)。當我運行代碼時,代碼中的索引是根據Items(食物列表)進行選擇,而不是從veges中選擇。謝謝你,如果你明白我的挑戰。

+0

你面臨的錯誤是什麼? – Bells

+0

你能顯示你的完整代碼嗎? –

+0

@Bells的錯誤是CarrotTypeName屬性在數據綁定返回空,因爲我不是能夠正確地從第二頁(該veges頁)導航。記住它綁定到項目列表中(食品名單) –

回答

1

解決方案是爲每個類(食物,蔬菜,胡蘿蔔)添加ID屬性。然後在Vege.xaml的SelectionChanged事件,我這樣做:

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

注:在我的ViewModel我創造了蔬菜的名單(含胡蘿蔔的列表中的每個蔬菜)的胡蘿蔔被稱爲VegeItems然後在Carrots.xaml

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; 


     } 
    } 

然後,在列表框中的的ItemSource,我把結合胡蘿蔔(紅蘿蔔的列表)蔬菜的屬性:.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> 
相關問題