绑定不工作Windows Phone 7

时间:2013-04-26 17:56:42

标签: c# 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中我看不到任何数据显示。请帮帮我

1 个答案:

答案 0 :(得分:0)

您的ViewModel有一个ObservableCollection项,您直接绑定到其中一个子项的属性。相反,你应该使用ListBox和模板绑定:

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