赞
踩
记录下每一个让我蛋疼的问题,说不定以后还会用得着。
关于wpf中listview的使用,
第一步:在xaml中使用listview,代码如下
- <ListView x:Name="listView" Width="800" Height="450" Margin="97,163,103,87" ItemsSource="{Binding}" >
- <ListView.View>
- <GridView>
- <GridViewColumn Header="序列号" Width="120" />
- <GridViewColumn Header="书名" Width="100"/>
- <GridViewColumn Header="类型" Width="80" />
- <GridViewColumn Header="作者" Width="100" />
- <GridViewColumn Header="出版社" Width="100" />
- <GridViewColumn Header="价格" Width="60" />
- <GridViewColumn Header="库存" Width="60" />
-
- </GridView>
- </ListView.View>
- </ListView>
然后界面大概是这个样子:
第二步:然后在.cs文件中加入如下代码(我的界面就叫SearchAndBuyWindow):
- public SearchAndBuyWindow()
- {
- InitializeComponent();
- initList();
- }
-
- public void initList()
- {
- for (int i = 0; i < 10; i++)
- {
- //listBook.Add(new Book(0,123456, "testBook"+i, "Math", "qiaobus", "shanghai", "just a book", "none"));
- listView1.Items.Add(new Book(0, 123456, "testBook" + i, "Math", "qiaobus", "shanghai", "just a book", "none"));
-
- }
- //listView.DataContext = listBook;
- }
加上之后,界面就不再空了,铛铛铛铛:
不过显示的数据不是我们需要的,这时候要回到xaml再做一些修改。
第三步:返回xaml修改GridViewColunm的属性如下:
- <ListView x:Name="listView1" Width="800" Height="450" Margin="97,199,103,51" ItemContainerStyle="{StaticResource ListViewItemStyle}">
- <ListView.View>
- <GridView>
- <GridViewColumn Header="序列号" Width="120" DisplayMemberBinding="{Binding book_id}"/>
- <GridViewColumn Header="书名" Width="100" DisplayMemberBinding="{Binding book_name}"/>
- <GridViewColumn Header="类型" Width="80" DisplayMemberBinding="{Binding book_type}"/>
- <GridViewColumn Header="作者" Width="100" DisplayMemberBinding="{Binding book_author}"/>
- <GridViewColumn Header="出版社" Width="100" DisplayMemberBinding="{Binding publisher}"/>
- <GridViewColumn Header="价格" Width="60" DisplayMemberBinding="{Binding book_price}"/>
- <GridViewColumn Header="库存" Width="60" DisplayMemberBinding="{Binding book_status}"/>
-
- </GridView>
- </ListView.View>
- </ListView>
增加了DisplayMemberBinding="{Binding book_name}"绑定某一项数据,
而我的ItemContainerStyle="{StaticResource ListViewItemStyle}"是为了使每个单元格内文字居中,待会我会在后面贴出。
加上代码后的界面就完成了:
下面是使单元格内文字居中的Syle代码:
- <Style x:Key="ListViewItemStyle" TargetType="{x:Type ListViewItem}">
- <Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
- </Style>
就是这么短一段,放在<Window.Resources></Window.Resources>标签内.
这是某位大神的博客,收藏一下:http://blog.csdn.net/tcjiaan/article/details/6949915
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。