【问题标题】:Binding Listbox Item Template to Variable将列表框项模板绑定到变量
【发布时间】:2016-08-11 13:45:25
【问题描述】:

所以我用这个 C# 代码将 Items 输入到列表框中:

     void GetItems() {
     VideoListBox.ItemsSource = from list1 in xmlfeedresult.Descendants("video")
                                   select new VideoItem
                                   {
                                       Username = list1.Element("u").Value,
                                       Thumbnail = list1.Element("i").Value,
                                       Description = list1.Element("d").Value
                                   };

    }

    public class VideoItem
    {
        public string Username;
        public string Thumbnail;
        public string Description;
    }

这是使用的 XAML 代码

<ListBox Height="588" Margin="272,101,272,0" Name="VideoListBox" VerticalAlignment="Top">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Height="132">
                    <Image Source="{Binding Thumbnail }" Height="300" Width="500" VerticalAlignment="Top" Margin="0,10,8,0"/>
                    <StackPanel Width="370">
                        <TextBlock Text="{Binding Username}" Foreground="#FFC8AB14" FontSize="28" />
                        <TextBlock Text="{Binding Description}" TextWrapping="Wrap" FontSize="24" />
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

问题是它在列表框中生成了三个项目,但是用户名、缩略图和描述中的值没有显示出来。它只是三个完全黑色的项目。 将不胜感激。

【问题讨论】:

    标签: c# wpf xaml listbox


    【解决方案1】:

    您的值应该是公开的属性,而不仅仅是字段。

    public class VideoItem
    {
        public string Username {get; set;}
        public string Thumbnail {get; set;}
        public string Description {get; set;}
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-28
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 2011-09-01
      相关资源
      最近更新 更多