2013-10-22 94 views
0

我有一個名爲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> 

有了這個代碼,我想將我的數據綁定到列表視圖。問題是當我運行這個代碼時,我的列表視圖的第二列是空的。任何人都可以幫助我嗎?

+0

不返回匿名類型。創建適當的數據模型或使用現有的數據模型。 –

+1

你可以更具體嗎? – Hooman

+1

我沒有看到'mydata'我看到一個匿名類型 –

回答

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); 

接下來,設置的ItemsSourceListView到集合

myListView.ItemsSource = Books; 

ListView應該是這個樣子:

<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> 

現在,而不是隱藏標識在第一列,你可以訪問它像這樣

var book = myListView.SelectedItem as Book; 
book.Id .... 

希望這有助於

+0

對不起,如果語法不正確我沒有IDE檢查它,但它假設是好的,如果我的記憶不是謊言 –

+0

它創建Observable collection時它給了我一個錯誤。它說它找不到。我應該使用任何命名空間嗎? – Hooman

+0

@ user1929674錯誤是什麼? –

0

感謝親愛的Omribitan代碼,問題出現在xaml的發佈者專欄(第二列)中。當我將DisplayMemberBinding =「{Binding Description}」更改爲DisplayMemberBinding =「{Binding description}」時,它工作正常。

相關問題