繁体   English   中英

包装在WINDOWS PHONE列表框中的c#链接对象

[英]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&amp;h=65&amp;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&amp;h=65&amp;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;

该代码之所以有效,是因为它们携带对象中的图像和链接,只是无法在我的列表框中显示。

  1. 具有可绑定的ObservableCollection<Item> (最好在ViewModel中)。
  2. 使用ListBox.ItemsSource属性绑定到所述ObservableCollection<Item> 标准绑定规则适用。
  3. ListBox每个项目都将代表绑定到控件的核心集合中的项目,因此以与其他任何方法相同的方式绑定到其属性。

回复评论:

好的,请再次阅读@Den发送给您的文章。 并请从ListBox删除DataContext Property ,将x:Name="myList"ListBox并在代码后方添加: myList.DataContext = this; 这不是最好的解决方案,但是很容易理解,首先您需要了解它:)最好的问候。

暂无
暂无

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

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