如何在ListBox中添加CheckBox

如何在ListBox中添加CheckBox,第1张

通过ListBox的Controls属性添加CheckBox时,要设置CheckBox的Location值,不然,添加多个CheckBox会只显示一个。

如下代码所示:

string[] list = new string[] { "张三", "李四", "王五" }

 

int x = 0, y = 0

foreach (string item in list)

{

      CheckBox cb = new CheckBox()

      cb.Text = item

      cb.Location = new Point(x, y)

      clbInvisibleColumn.Controls.Add(cb)

      y += 22

}

在你的代码之前加代码:

Thisform.Caption=This.Picture[This.ListIndex]

你会发现系统自动把路径加上了。

所以解决方法是把

IF This.Picture[This.ListIndex] = "checkbox0.png" Then

改为:

IF "checkbox0.png" $ This.Picture[This.ListIndex] Then

当然可以,wpf以 Content来进行加载界面的,Content可是Object对象,只有是满足其继承框架的控件都可以添加进来

<Window x:Class="WpfDemo.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        title="MainWindow" Height="350" Width="525">

    <Grid>

        <ListBox>

            <ListBoxItem >

                <CheckBox Content="CheckBox"></CheckBox>

            </ListBoxItem>

            <ListBoxItem >

                <Button Content="Button"></Button>

            </ListBoxItem>

            <ListBoxItem >

                <TextBlock Text="TextBlock"></TextBlock>

            </ListBoxItem>

            <ListBoxItem >

                <StackPanel Orientation="Horizontal">

                    <Image Source="F:/Icon/fileopen.png"></Image>

                    <CheckBox Content="CheckBox" VerticalAlignment="Center"></CheckBox>

                    <Button Content="Button"></Button>

                    <TextBox Text="TextBox"></TextBox>

                </StackPanel>

            </ListBoxItem>

        </ListBox>

    </Grid>

</Window>


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11220664.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-14
下一篇2023-05-14

发表评论

登录后才能评论

评论列表(0条)

    保存