Windows phone 7无法绑定

本文关键字:绑定 phone Windows | 更新日期: 2023-09-27 18:11:14

我想绑定一些数据。我有一份名单。每点击一个,我就把用户带到详细信息页面。

在点击每个项目时,我从网络上获取数据并对其进行解析。

    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 . example .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中我看不到任何数据被显示。

Windows phone 7无法绑定

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