我有一個名爲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>
有了這個代碼,我想將我的數據綁定到列表視圖。問題是當我運行這個代碼時,我的列表視圖的第二列是空的。任何人都可以幫助我嗎?
不返回匿名類型。創建適當的數據模型或使用現有的數據模型。 –
你可以更具體嗎? – Hooman
我沒有看到'mydata'我看到一個匿名類型 –