我也有一个ListView
包含控件的GridView
控件。数据来自数据库数据库方案的不同表:
- 书籍(BookID、BookName、ISBN、CopyLeft);
- 借款人(BorrowerID、BorrowerName、BorrowerLevel);
- 交易(TransactionID、BorrowerID、BookID、BorrowDate、ReturnDate、IsReturned);
对应的类分别Book
是Borrower
和Transaction
。
使用数据绑定从这些表中获取GridView
、
TransactionID
、BorrowerID
、BorrowerName
和。XAML 代码:BookName
BorrowDate
ReturnDate
<Grid>
<ListView Margin="15,57,58,57" Name="borrowedBookList" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding }" KeyDown="borrowedBookList_KeyDown">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="80" Header="Borrower ID" DisplayMemberBinding="{Binding Path=BorrowerID}"/>
<GridViewColumn Width="220" Header="Borrower Name" DisplayMemberBinding="{Binding Path=BorrowerName}"/>
<GridViewColumn Width="220" Header="Book Name" DisplayMemberBinding="{Binding Path=BookName}"/>
<GridViewColumn Width="100" Header="Date" DisplayMemberBinding="{Binding Path=BorrowDate}"/>
<GridViewColumn Width="100" Header="Return Date" DisplayMemberBinding="{Binding Path=ReturnDate}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
<Label Content="Borrowed Book List" Height="34" HorizontalAlignment="Left" Margin="310,12,0,0" Name="label1" VerticalAlignment="Top" Width="151" />
<Button Content="Close" Height="31" HorizontalAlignment="Left" Margin="636,535,0,0" Name="close" VerticalAlignment="Top" Width="106" Click="close_Click"/>
<Button Content="Return This Book" Height="31" HorizontalAlignment="Left" Margin="454,535,0,0" Name="return" VerticalAlignment="Top" Width="115" Click="return_Click"/>
</Grid>
现在,我可以选择一行作为Transaction
orBook
或的对象Borrower
吗?我的目标是选择一行并识别Transactions
,Books
表的相应条目并递增CopyLeft
并将“YES”分配给IsReturned
属性。那么我该怎么做呢?
另外:我尝试这样编写代码:
var selectedTran = this.borrowedBookList.selectedItem as Transaction;
var selectedBook=this.borrowedBookList.selectedItem as Book;
try
{
selectedBook.CopyLeft++;
selectedTran.isReturned="YES";
}
catch(Exception ex)
{
MessageBox.Show(ex.message);
}
异常显示“对象引用未设置为对象的实例”。然后我试图像这样找到所借书列表的类型:
MessageBox.Show(borrowedBookList.SelectedItem.GetType().ToString());
它显示了一个匿名类型。