繁体   English   中英

绑定无法正常工作的Windows Phone 7

[英]binding not working 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中,我看不到任何正在显示的数据。 请帮我

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM