需要帮助在powershell中向listview添加复选框

时间:2016-01-12 18:03:36

标签: wpf listview powershell

我目前正在制作一个能够检索SQL数据库并备份它们的GUI。我的ListView将填充数据库,但我想显示每个数据库旁边的复选框。这将允许用户仅备份所需的数据库。

在Visual Studio中,我在listview中添加了4个复选框(只是为了解决这些问题并了解如何使它们工作)。我不确定如何在列表数据旁边显示powershell show复选框,因为默认情况下我什么也看不见。

这就是我的WPF代码

<Window x:Class="SQLGUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SQLGUI"
        mc:Ignorable="d"
        Title="MainWindow" Height="547.722" Width="861.104" Background="#FF4F4A4A">
    <Grid>
        <ComboBox x:Name="instance" HorizontalAlignment="Left" Margin="10,76,0,0" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="selectdatabase" Content="Load Database" HorizontalAlignment="Left" Margin="135,76,0,0" VerticalAlignment="Top" Width="92"/>
        <ListView x:Name="listView" HorizontalAlignment="Left" Height="350" Margin="10,103,0,0" VerticalAlignment="Top" Width="812">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Database Name" DisplayMemberBinding ="{Binding 'Database Name'}" Width="200"/>
                    <GridViewColumn Header="Created" DisplayMemberBinding ="{Binding 'Created'}" Width="200"/>
                    <GridViewColumn Header="Last Backup" DisplayMemberBinding ="{Binding 'Last Backup'}" Width="200"/>
                    <GridViewColumn Header="Size(KB)" DisplayMemberBinding ="{Binding 'Size(KB)'}" Width="200"/>
                </GridView>
            </ListView.View>
            <CheckBox x:Name="box1" Content="Test"/>
            <CheckBox x:Name="box2" Content="Test 2"/>
            <CheckBox x:Name="box3" Content="Test 3"/>
            <CheckBox x:Name="box4" Content="Test 4"/>
        </ListView>
        <Button x:Name="backup" Content="Backup" HorizontalAlignment="Left" Margin="130,482,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>

这就是我在WPF上制表数据的方法

$WPFselectdatabase.add_click({
$WPFlistView.items.clear()
[reflection.assembly]::LoadWithPartialName("Microsoft.Sqlserver.smo")
$sqlserver=New-Object ("Microsoft.Sqlserver.management.smo.server") "MS-SQL"
foreach($sqldatabase in $sqlserver.databases){
$sqldatabase.name | select-object @{Name='Database Name';EX={$sqldatabase.name}}, @{Name='Created';Ex={$sqldatabase.CreateDate}},@{Name='Last Backup';Ex={$sqldatabase.LastBackupDate}},@{Name='Size(KB)'; EX={($sqldatabase.Size)}} | % {$WPFlistView.AddChild($_)}}
})

2 个答案:

答案 0 :(得分:0)

ListView.CheckBoxes Property

我认为您需要将CheckBoxes属性添加到ListView元素:

<ListView x:Name="listView" CheckBoxes="True" HorizontalAlignment="Left" Height="350" Margin="10,103,0,0" VerticalAlignment="Top" Width="812">

答案 1 :(得分:0)

搜索堆栈溢出后,我能够找到解决方案!

似乎需要使用DataTemplate添加新的GridView列。添加以下代码解决了这个问题。

<GridViewColumn>
   <GridViewColumn.CellTemplate>
     <DataTemplate>
       <CheckBox />
    </DataTemplate>
   </GridViewColumn.CellTemplate>
</GridViewColumn>

所以最终的GUI代码最终看起来像

<Window x:Class="SQLGUI.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:SQLGUI"
        mc:Ignorable="d"
        Title="MainWindow" Height="547.722" Width="861.104" Background="#FF4F4A4A">
    <Grid>
        <ComboBox x:Name="instance" HorizontalAlignment="Left" Margin="10,76,0,0" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="selectdatabase" Content="Load Database" HorizontalAlignment="Left" Margin="135,76,0,0" VerticalAlignment="Top" Width="92"/>
        <ListView x:Name="listView" HorizontalAlignment="Left" Height="350" Margin="10,103,0,0" VerticalAlignment="Top" Width="834">
            <ListView.View>
                <GridView>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Database Name" DisplayMemberBinding ="{Binding 'Database Name'}" Width="300"/>
                    <GridViewColumn Header="Created" DisplayMemberBinding ="{Binding 'Created'}" Width="200"/>
                    <GridViewColumn Header="Last Backup" DisplayMemberBinding ="{Binding 'Last Backup'}" Width="200"/>
                    <GridViewColumn Header="Size" DisplayMemberBinding ="{Binding 'Size(MB)'}" Width="150"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button x:Name="backup" Content="Backup" HorizontalAlignment="Left" Margin="130,482,0,0" VerticalAlignment="Top" Width="75"/>

    </Grid>
</Window>