获取列表项的问题 [英] Problem with getting listview item
本文介绍了获取列表项的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个wpf应用程序,里面有一个listview.我使用了for循环来生成列表视图的项目,但是我的问题是我的item.count为17,我只能获取行0 1 2的值,而其他行为null,我不得不提到我可以显示信息设计模式下listview内的所有行(名称,照片等),但是在后面的代码中,第3行及之后的行为null,有人可以指导我我错了我的问题吗?非常感谢.
解决方案
您的代码已损坏.坦率地说,Tanx Christian.让我发表我的代码的某些部分:
//这是我填写列表视图的一部分:
DataSet da = new DataSet();
connRead.Fill(da);
listView.DataContext = da.Tables [0];
//这部分是当我想获得商品的价值时:
ListViewItem容器= null;
for(int i = 0; i< listView.Items.Count; i ++)
{
container = listView.ItemContainerGenerator.ContainerFromIndex(i)as ListViewItem;
if(container!= null)//在这里,对于i = 3及更高版本,我得到null值
{
...
}
这是我的xaml部分:
< ListView x:Name =" span> 高度 =" 202" ItemsSource {Binding}" SelectionChanged =" HorizontalAlignment 左" 保证金 =" 20,84,0,0" VerticalAlignment =" =" 435" IsSynchronizedWithCurrentItem =" DataContext {Binding}" < ListView.ItemTemplate > < DataTemplate > < 网格 =" 网格" < Grid.RowDefinitions > < RowDefinition =" 自动" > < RowDefinition =" 自动" > < RowDefinition =" 50" > < RowDefinition =" 自动" > < RowDefinition =" 自动" > < /Grid.RowDefinitions > < Grid.ColumnDefinitions > < ColumnDefinition/ > < ColumnDefinition/ > < ColumnDefinition/ > < ColumnDefinition/ > < /Grid.ColumnDefinitions > < 图片 =" image1" 宽度 70 " 高度 110" Grid.Row =" =" 1" =" {Binding PhotoName}" ">< /图像 > < 标签 =" 电子邮件" Grid.Row 0" Grid.Column 2" 内容 =" > < /Label > < 标签 =" 名称" Grid.Row 1" Grid.Column 2" 内容 =" > < /Label > < 复选框 =" chB" Grid.Row 0" Grid.Column 0" > < /CheckBox > < /Grid > < /DataTemplate > < /ListView.ItemTemplate > < /ListView >
Hi,I have a wpf application and inside that i have a listview . I used a for loop to generate the items of my listview but my problem is whereas my item.count is 17,I just can get value for rows 0 1 2 and other rows are null,I have to mention that I can display information for all rows inside my listview in design mode(names,photos,...) but in code behind i get null for rows 3 and after,can someone guide me what is my problem where I am wrong?thank you so much.
解决方案
Your code is broken. Hard to say more without seeing it.
Tanx Christian for your reply.let me post some parts of my code:
//here is the part when i fill up my listview:
DataSet da = new DataSet();
connRead.Fill(da);
listView.DataContext = da.Tables[0];
//this part is for when i want to get my items value:
ListViewItem container = null;
for (int i = 0; i < listView.Items.Count;i++ )
{
container = listView.ItemContainerGenerator.ContainerFromIndex(i) as ListViewItem;
if (container != null)//here for i = 3 and onward i get null value
{
...
}
this is my xaml part:
<ListView x:Name="listView" Height="202" ItemsSource="{Binding}" SelectionChanged="listView_SelectionChanged" HorizontalAlignment="Left" Margin="20,84,0,0" VerticalAlignment="Top" Width="435" IsSynchronizedWithCurrentItem="{x:Null}" DataContext="{Binding}"> <ListView.ItemTemplate> <DataTemplate> <Grid x:Name="grid"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="50"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Image x:Name="image1" Width="70" Height="110" Grid.Row="1" Grid.Column="1" Source="{Binding PhotoName}"></Image> <Label x:Name="email" Grid.Row="0" Grid.Column="2" Content="{Binding Email}"></Label> <Label x:Name="name" Grid.Row="1" Grid.Column="2" Content="{Binding Name}"></Label> <CheckBox x:Name="chB" Grid.Row="0" Grid.Column="0" ></CheckBox> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView>
这篇关于获取列表项的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文