我试图动态地将复选框添加到VB中的lisview子项 [英] I am trying to add checkbox dynamically to the lisview subitem in VB

查看:62
本文介绍了我试图动态地将复选框添加到VB中的lisview子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动态添加复选框到listview作为vb中的子项我该怎么做?我的代码无效..它在listview中的相同位置创建了三个复选框。我想逐个添加每一行的复选框..请帮助



我尝试了什么:



 READER = command1.ExecuteReader 

While READER.Read()
Dim item As New ListViewItem(READER.GetString( hos_id))
item.SubItems.Add(READER.GetString(name))
item.SubItems.Add(READER.GetString(room_no))
chk.Name = READER.GetInt32(hos_id)
chk.Text = READER.GetInt32(hos_id)
chk.Width = 70
ListView1.Controls.Add(chk)
AddHandler chk.CheckedChanged,AddressOf ChkBox_CheckedChanged
ListView1.Items.Add(item)
End

解决方案

< blockquote>Prakash。



为了正确,因为我没有使用Listviews。



项目应该是列表视图中的条目/>


项目本身应包含子项目?



是否也可以将复选框用作子项目?



所以这样:

 items.Subitems.add(chk)??? 





并删除该行:

 ListView1.Controls.Add(chk)





如果你需要3个复选框? (我没有看到其中的三个。)



使用适当的变量名重复items.Subitems.add(chk)行。



也许在Readline内有一个循环。阅读。



我会尝试只有一个项目,其中包含复选框和全部从数据集中读出的其他内容(捆绑到一个数据集的数据)。



所以如果

引用:

ListViewItem

可以有子项,并且可以在其中包含控件,我会将所有数据放在控件中。

所以喜欢

Quote:

item.SubItems.Add(READER.GetString(name))

item.SubItems.Add(READER .GetString(room_no))





将成为例如:

 item.Controls .Add(New Lable_xxx.Text = READER.GetString(name)
item.Controls.Add(New Lable_xxy.Text = READER.GetString(room_no)










如果您使用的是WPF或UWP,请尝试使用类和Datatemplates。



类是读取器读入对象的数据。



这些应该进入一个ObservableCollection(你的Classtype)



然后ListView将被绑定到这个Collection(ItemsSource)。



在Xaml中,Datatemplate可能如下所示:



< DataTemplate x:Key =LindenstrasseTemplate
DataType = 本地:LindenstrasseWikiPediaEpisode >
< Border BorderBrush =Black
BorderThickness =2
CornerRadius =4
d:DesignHeight =150
d:DesignWidth =600 >
< Grid>
< Grid.RowDefinitions>
< RowDefinition Height =1 */>
< RowDefinition Height =1 */>
< RowDefinition Height =1 */>
< /Grid.RowDefinitions>
< Label Content ={Binding Titel}
Margin =4
FontSize =36
FontWeight =Bold/>
< Grid Grid.Row =1>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =120/>
< ColumnDefinition Width =80/>
< ColumnDefinition Width =80/>
< ColumnDefinition Width =Auto/>
< /Grid.ColumnDefinitions>
< ProgressBar IsIndeterminate ={Binding Downloading,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}
Grid.ColumnSpan =2
Grid.Column =1
Height =28
Margin =4,0
Visibility ={Binding Downloading,Converter = {StaticResource BooleanToVisibilityConverter},Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}/>
< CheckBox Style ={StaticResource OrangeSwitchStyle}
IsChecked ={Binding DateiVorhanden,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}
Margin =10,0,4,0
Grid.Column =0
Horizo​​ntalAlignment =Left/>
< Label Grid.Column =1
Content ={Binding StaffelFolge,FallbackValue = 999}
Width =Auto/>
< Label Content ={Binding Folge}
Width =Auto
Grid.Column =2/>
< Label Content ={Binding Sendedatum}
Horizo​​ntalAlignment =Stretch
Width =Auto
Grid.Column =3/>
< / Grid>
< Label Grid.Column =0
Content ={Binding Dateiname}
Margin =4
Grid.Row =2/>

< / Grid>
< / Border>
< / DataTemplate>





列表框是我的示例,但您可以使用ListViewControl,并设置 ItemsSource 到ObservableCollection,并且还设置了 ItemTemplate ,ListView可以相应地渲染ListViewItems。

(将一个变量/属性的SelectedItem设置为获取所选的所有信息(数据集)仅当您使用MVVM时



< ListBox x:Name =LBLindenstrasse
VirtualizingPanel.IsVirtualizingWhenGrouping =True
AlternationCount =3
Grid.Column =0
ItemsSource ={Binding LindenstrasseListe.DieEpisoden,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged }
ItemTemplate ={StaticResource LindenstrasseTemplate}
Horizo​​ntalContentAlignment =Stretch
SelectedItem ={BindingLindenstrasseAusgewählte Episode,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}
IsSynchronizedWithCurrentItem =True
Background =#3F7742B5
Grid.Row =1>







如果需要,请详细询问DataTemplates。



cu来自汉堡的Zui从未使用过ListView(嗯,也许我应该尝试一次,有时...)


adding checkbox dynamically to the listview as sub-item in vb how do i do it ? my code is not working.. it create three checkbox at same place in listview. i want to add the checkbox at each row one by one..please help

What I have tried:

READER = command1.ExecuteReader

           While READER.Read()
               Dim item As New ListViewItem(READER.GetString("hos_id"))
               item.SubItems.Add(READER.GetString("name"))
               item.SubItems.Add(READER.GetString("room_no"))
               chk.Name = READER.GetInt32("hos_id")
               chk.Text = READER.GetInt32("hos_id")
               chk.Width = 70
               ListView1.Controls.Add(chk)
               AddHandler chk.CheckedChanged, AddressOf ChkBox_CheckedChanged
               ListView1.Items.Add(item)
           End While

解决方案

Hi, Prakash.

To get it right, because I don´t use Listviews.

The "item" should be an entry inside the Listview.

The item itself should contain "subitems" ?

Is it possible to use the Checkbox also as a Subitem?

So like this:

items.Subitems.add(chk) ???



and remove the line:

ListView1.Controls.Add(chk)



And if you need 3 checkboxes? (I don´t see three of them.)

Repeat the items.Subitems.add(chk) line with appropriate variablenames.

Maybe with a Loop inside the Readline.Read.

I would try to have only one Item which would contain the checkboxes and the all other stuff that is read out of a "Dataset" (the Data that are bundled to one Set).

So if the

Quote:

ListViewItem

can have Subitems and can have Controls inside of it, I would do all Data placed in Controls.
So like

Quote:

item.SubItems.Add(READER.GetString("name"))
item.SubItems.Add(READER.GetString("room_no"))



Will become for example:

item.Controls.Add(New Lable_xxx.Text = READER.GetString("name")
item.Controls.Add(New Lable_xxy.Text = READER.GetString("room_no")






If You are using WPF or UWP, please try to use Classes and Datatemplates.

Classes are the Data read out of the Reader into Objects.

These should go into an ObservableCollection (that is of Your Classtype)

then the ListView will be bound to this Collection (ItemsSource).

In Xaml the Datatemplate could look like this:

<DataTemplate x:Key="LindenstrasseTemplate"
                      DataType="local:LindenstrasseWikiPediaEpisode">
            <Border BorderBrush="Black"
                    BorderThickness="2"
                    CornerRadius="4"
                    d:DesignHeight="150"
                    d:DesignWidth="600">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>
                    <Label Content="{Binding Titel}"
                           Margin="4"
                           FontSize="36"
                           FontWeight="Bold" />
                    <Grid  Grid.Row="1">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="120" />
                            <ColumnDefinition Width="80" />
                            <ColumnDefinition Width="80" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <ProgressBar IsIndeterminate="{Binding Downloading, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                     Grid.ColumnSpan="2"
                                     Grid.Column="1"
                                     Height="28"
                                     Margin="4,0"
                                     Visibility="{Binding Downloading, Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        <CheckBox Style="{StaticResource OrangeSwitchStyle}"
                                  IsChecked="{Binding DateiVorhanden, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                  Margin="10,0,4,0"
                                  Grid.Column="0"
                                  HorizontalAlignment="Left" />
                        <Label Grid.Column="1"
                               Content="{Binding StaffelFolge, FallbackValue=999}"
                               Width="Auto" />
                        <Label Content="{Binding Folge}"
                               Width="Auto"
                               Grid.Column="2" />
                        <Label Content="{Binding Sendedatum}"
                               HorizontalAlignment="Stretch"
                               Width="Auto"
                               Grid.Column="3" />
                    </Grid>
                    <Label Grid.Column="0"
                           Content="{Binding Dateiname}"
                           Margin="4"
                           Grid.Row="2" />

                </Grid>
            </Border>
        </DataTemplate>



The Listbox is my Example, but you can use the ListViewControl, and set the ItemsSource to the ObservableCollection, and also set the ItemTemplate, that the ListView can render the ListViewItems acoordingly.
("The SelectedItem to a Variable/Property to get all Information of the Selected (Dataset)" Only if You are using MVVM)

<ListBox x:Name="LBLindenstrasse"
                     VirtualizingPanel.IsVirtualizingWhenGrouping="True"
                     AlternationCount="3"
                     Grid.Column="0"
                     ItemsSource="{Binding LindenstrasseListe.DieEpisoden, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                     ItemTemplate="{StaticResource LindenstrasseTemplate}"
                     HorizontalContentAlignment="Stretch"
                     SelectedItem="{Binding LindenstrasseAusgewählteEpisode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                     IsSynchronizedWithCurrentItem="True"
                     Background="#3F7742B5"
                     Grid.Row="1">




Please ask more about DataTemplates if needed.

c.u. Zui from Hamburg, who never used a ListView (hmm, maybe i should try one, sometime...)


这篇关于我试图动态地将复选框添加到VB中的lisview子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆