2013-04-26 80 views
0

嗨,大家好我正試圖綁定一些數據。 我有一個人的名單。點擊每個這些我把用戶的詳細信息頁面。綁定不工作windows phone 7

每個項目的水龍頭,我從網絡獲取數據和解析它。

public ObservableCollection<ItemViewModel> PeopleDetails { get; set; } 

上述行我在MainViewModel宣佈

ItemViewModel.cs

在每個項目上的自來水
public class ItemViewModel : INotifyPropertyChanged 
    { 

     private string _person_name; 
    public string _Person_name 
    { 
     get { return _person_name; } 

     set 
     { 
      if (value != _person_name) 
      { 
       _person_name= value; 
       NotifyPropertyChanged("_Person_name"); 
      } 
     } 
    } 

    private string _person_info; 
    public string _Person_info 
    { 
     get { return _person_info; } 

     set 
     { 
      if (value != _person_info) 
      { 
       _person_info= value; 
       NotifyPropertyChanged("_Person_info"); 
      } 
     } 
    } 

    private string _person_image_link; 
    public string _Person_image_link 
    { 
     get { return _person_image_link; } 
     set 
     { 
      if (value != _person_image_link) 
      { 
       _person_image_link= value; 
       NotifyPropertyChanged("_Person_image_link"); 
      } 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (null != handler) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    } 

是執行以下代碼

 private void getPeopleDetails(object sender, SelectionChangedEventArgs e) 
    { 

     // Navigate to the new page 
     if (e.AddedItems != null && e.AddedItems.Count == 1) 
     { 
      People selectedItem = (People)e.AddedItems[0]; 
      NavigationService.Navigate(new Uri("/PeopleDetailsView.xaml?id="+selectedItem.id, UriKind.Relative)); 


     } 
    } 

然後PeopleDetailsView .xaml.cs我的代碼如下

 public PeopleDetailsView() 
    { 
     DataContext = App.Model; 
     InitializeComponent(); 
     Loaded+=new RoutedEventHandler(PeopleDetailsView_Loaded); 
    } 

    private void PeopleDetailsView_Loaded(Object sender ,RoutedEventArgs e){ 
     string id = ""; 
     if (NavigationContext.QueryString.TryGetValue("id",out id)) 
     { 
      string url = "*****&id=" + id;"; 

      WebClient client = new WebClient(); 
      client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompletedC); 
      client.DownloadStringAsync(new Uri(url)); 
     } 
     } 

    private void client_DownloadStringCompletedC(object sender, DownloadStringCompletedEventArgs e) 
    { 
     try 
     { 

      JToken a = JObject.Parse(e.Result); 
      App.Model.PeopleDetails.Add(
      new ItemViewModel() 
      { 
       _Person_info = a.SelectToken("info").ToString(), 
       _Person_image_link = a.SelectToken("image_link").ToString(), 
       _Person_name = a.SelectToken("name").ToString(), 
      } 
      ); 

     } 
     catch (Exception execp) 
     { 
      MessageBox.Show(execp.Message.ToString()); 
     } 

    } 

,最後我在我的PeopleDetailsView.xaml結合數據如下

 <Grid x:Name="ContentPanel" DataContext="{Binding PeopleDetails}" Grid.Row="1" Margin="12,0,12,0"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="269*" /> 
      <RowDefinition Height="338*" /> 
     </Grid.RowDefinitions> 
     <Image Height="250" HorizontalAlignment="Left" Margin="12,6,0,0" Name="image1" Stretch="Fill" Source="{Binding _Person_image_link}" VerticalAlignment="Top" Width="246" /> 
     <ScrollViewer Grid.Row="1" Height="274" HorizontalAlignment="Left" Margin="12,42,0,0" Name="scrollViewer1" VerticalAlignment="Top" Width="420"> 
      <TextBlock Height="264" Name="textBlock1" Text="{Binding _Person_info}" /> 
     </ScrollViewer> 
    </Grid> 
</Grid> 

但在PeopleDetailsView我看不到顯示任何數據。請幫我

+0

你能告訴我們一些更多的信息?我們需要看到你的ViewModel(ItemViewModel),你是否確定遠程方法實際返回數據,並且你正確解析它? – Kenneth 2013-04-26 18:14:20

+0

是的,我也正確地獲取數據並解析它。確定il添加ItemViewModel – nyfer 2013-04-26 18:16:52

回答

0

你是ViewModel有一個ObservableCollection項目,你直接綁定到一個孩子的屬性。相反,你應該使用列表框和模板綁定:

<ListBox x:Name="ContentPanel" ItemsSource="{Binding PeopleDetails}" Grid.Row="1" Margin="12,0,12,0"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Image Height="250" HorizontalAlignment="Left" Margin="12,6,0,0" Name="image1" Stretch="Fill" Source="{Binding _Person_image_link}" VerticalAlignment="Top" Width="246" /> 
      <ScrollViewer Grid.Row="1" Height="274" HorizontalAlignment="Left" Margin="12,42,0,0" Name="scrollViewer1" VerticalAlignment="Top" Width="420"> 
       <TextBlock Height="264" Name="textBlock1" Text="{Binding _Person_info}" /> 
     </ScrollViewer> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

好的,我需要ObservableCollection這裏的項目嗎? – nyfer 2013-04-26 18:38:50

+0

是的,否則當你添加一個項目時它不會更新。如果你使用的例子,我只是給它應該工作 – Kenneth 2013-04-26 18:39:42

+0

確定它的工作原理。非常感謝 :) – nyfer 2013-04-26 18:52:04