[英]c# link objects wrapped on the listbox in WINDOWS PHONE
我有這段代碼,您可以在屏幕打印中看到正確地加載所需的數據並存儲對象列表PopularVideos:
item { Title = Hey Porsche, Url = http://www.unnu.com/wp-content/plugins/wordpress-popular-posts/timthumb.php?src=http://www.unnu.com/wp-content/uploads/2013/03/019.jpg&h=65&w=275 } <>f__AnonymousType0<string,string>
item.Title "Hey Porsche" string
item.Url "http://www.unnu.com/wp-content/plugins/wordpress-popular-posts/timthumb.php?src=http://www.unnu.com/wp-content/uploads/2013/03/019.jpg&h=65&w=275" string
需要將這些對象加載到我的列表框中並進行綁定,否則也可以。 但是Windows Phone不適用於DataSource和DisplayMember。
我的XAML:
<ListBox Name="listBoxPopular">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Name="imagem" Source="{Binding Path=Url}"/>
<TextBlock Text="{Binding Titulo}" Tap="HyperlinkButton_Tap" FontSize="30" Foreground="#FF159DDE" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PanoramaItem>
我的課程是:
class PopularVideos
{
public PopularVideos() { }
public PopularVideos(string titulo, string url)
{
Titulo = titulo;
BitmapImage Img = new BitmapImage(new Uri(url));
}
public string Titulo { get; set; }
public Uri Url { get; set; }
}
我的代碼背后是:
_popVideos = new List<PopularVideos>();
var data = e.Document.DocumentNode.SelectSingleNode("//div[@class='content']")
.Descendants("img")
.Select(img => new
{
Title = img.Attributes["alt"].Value,
Url = img.Attributes["src"].Value,
}).ToList();
foreach (var item in data)
{
PopularVideos pop = new PopularVideos(item.Title, item.Url);
_popVideos.Add(new PopularVideos(item.Title, item.Url));
}
listBoxPopular.ItemsSource = _popVideos;
該代碼之所以有效,是因為它們攜帶對象中的圖像和鏈接,只是無法在我的列表框中顯示。
ObservableCollection<Item>
(最好在ViewModel中)。 ListBox.ItemsSource
屬性綁定到所述ObservableCollection<Item>
。 標准綁定規則適用。 ListBox
每個項目都將代表綁定到控件的核心集合中的項目,因此以與其他任何方法相同的方式綁定到其屬性。 回復評論:
好的,請再次閱讀@Den發送給您的文章。 並請從ListBox
刪除DataContext
Property
,將x:Name="myList"
到ListBox
並在代碼后方添加: myList.DataContext = this;
這不是最好的解決方案,但是很容易理解,首先您需要了解它:)最好的問候。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.