2016-01-12 104 views
0

我目前正在製作一個GUI,將檢索SQL數據庫並備份它們。我的ListView將填充數據庫,但我想顯示每個數據庫旁邊的複選框。這將允許用戶僅備份期望的數據庫。需要幫助添加複選框到列表視圖在powershell

在Visual Studio中,我添加了4個複選框(僅用於播放並瞭解如何使它們工作)到我的列表視圖。我不完全確定我可以如何在列表數據旁邊顯示覆選框,因爲默認情況下我什麼都看不到。

這是我的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($_)}} 
}) 
+0

這是什麼我什...你...編碼UI WPF的PowerShell ......?是吧?我感覺暈眩。我需要躺下。你能解釋一下PowerShell在你的問題中做了什麼?它可能有助於頭暈... – Will

+0

哇。一個真正的問題 - 您是否在PowerShell中使用WPF,因爲您正在開發PowerShell應用程序並使用WPF進行復雜輸入,或者您是否編寫了WPF應用程序,並認爲通過PowerShell對它們進行編碼是正常的? – Will

+0

@將我試圖自動化SQL數據庫的備份,我使用powershell的原因是因爲在過去,使用預編譯的.NET或C#應用程序已經造成麻煩,並且無法在移動中解決問題。我使用PowerShell的原因很簡單,因爲它可以在旅途中進行修復和修改,我使用WPF來確保最終用戶的最大簡化 - 儘管以我爲代價的主要複雜性:P – Bregs

回答

0

ListView.CheckBoxes Property

我認爲你需要添加的複選框屬性到ListView元素:

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

不幸的是,沒有工作。添加該參數會導致GUI無法加載。 – Bregs

0

通過堆棧溢出搜索後,我能夠找到解決方案!

看起來,一個新的GridView列需要添加一個DataTemplate。添加下面的代碼解決了這個問題。

<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>