[英]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.