将多个自定义项插入listview

时间:2013-10-22 16:03:54

标签: c# wpf linq

我有一个名为Books的课程。代码在这里:

class Books
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string description { get; set; }    
    }

我的表单背后的代码如下所示:(感谢Omribitan)

 public partial class MainWindow : Window
    {

        private ObservableCollection<Books> Books { get; set; }
        public MainWindow()
        {
            InitializeComponent();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var db = new DataClasses1DataContext();
            var query = from p in db.books
                        select new Books
                        {
                            Id = p.id,
                            Name = p.name,
                            description = p.description
                        };

            Books = new ObservableCollection<Books>(query);
            listview1.ItemsSource = Books;

            }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var book = listview1.SelectedItem as Books;
            MessageBox.Show(book.Id.ToString());
        }

XAML看起来像这样:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:test" xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="test.MainWindow"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <ListView x:Name="listview1" HorizontalAlignment="Left" Height="100" Margin="126,91,0,0" VerticalAlignment="Top" Width="286">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="BookName"
                      Width="Auto"
               DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Publisher"
                      Width="Auto"
               DisplayMemberBinding="{Binding Description}" />
                </GridView>
            </ListView.View>
        </ListView>

使用此代码,我想将数据绑定到listview。问题是,当我运行此代码时,listview的第二列是空的。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

首先像@HighCore建议的那样,创建一个正确的数据类型而不是匿名类型。

public class Book
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

创建一个集合,例如ObservableCollection

public ObservableCollection<Book> Books { get; set; }

将您的商品添加到收藏集

var db=new mydataDataContext();
var query=from p in db.Book
select new Book{
            Id = p.Id
            Name = P.Name
            Description = P.Description
           };
Books = new ObservableCollection<Book>(query);

接下来,将ItemsSource的{​​{1}}设置为集合

ListView

您的myListView.ItemsSource = Books; 应该是这样的:

ListView

现在,您可以像这样

访问它,而不是将Id隐藏在第一列中
<ListView x:Name="myListView">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="BookName"
                      Width="Auto"
               DisplayMemberBinding="{Binding Name}" />
      <GridViewColumn Header="Publisher"
                      Width="Auto"
               DisplayMemberBinding="{Binding Description}" />
    </GridView>
  </ListView.View>
</ListView>

希望这有帮助

答案 1 :(得分:0)

感谢亲爱的Omribitan代码,问题出在xaml的发布者专栏(第二列)中。当我将DisplayMemberBinding =“{Binding Description}”更改为DisplayMemberBinding =“{Binding description}”时,它工作正常。