我是PowerShell中WPF的新手。我正在构建一个小应用程序,通过从表单中选择数据库来执行按需SQL备份,如此捕获中所示。 WPF PowerShell Form
我的表单正常,能够进行备份。我现在正在尝试禁用备份/恢复按钮,以便仅在选中至少一个复选框时才可以单击它们。这需要将备份按钮绑定到复选框(在我的理解中)。我已经能够找到一种方法将它与一个复选框绑定,但无法找到一种方法来执行多个复选框。我的XAML复选框和按钮如下所示。
<CheckBox Name="UAT_DB1" Content="UAT-DB1" HorizontalAlignment="Left" Margin="21,113,0,0" VerticalAlignment="Top" Cursor=""/>
<CheckBox Name="UAT_DB2" Content="UAT-DB2" HorizontalAlignment="Left" Margin="21,140,0,0" VerticalAlignment="Top"/>
<Button Name="btn_backup" Content="Backup" HorizontalAlignment="Left" Height="29" Margin="238,343,0,0" VerticalAlignment="Top" Width="78" IsEnabled="{Binding ElementName=UAT_DB1, Path=IsChecked}"/>
如果我选择了DB1,则启用该按钮并单击,检查从所有复选框中选择的内容并备份所有这些数据库。下面是备份按钮上的点击功能代码。
$WPFbtn_backup.Add_Click(
{
foreach($var in $vars)
{
if($var.Name -match "WPFUAT" -and $var.Value.IsChecked -eq $true)
{
$name = $var.Value.Content
$dbs = $dbs + $name
}
}
if ($dbs.Length -gt 0)
{
$msg = $dbs -join "`n"
$confirmation = [System.Windows.MessageBox]::Show("Following DBs will be backed up.`n`n$msg",'Tipper','OKCancel','Info')
}
else
{
[System.Windows.MessageBox]::Show("`nYou have to select a DB to backup or restore.",'Tipper','OK','Warning')
}
switch($confirmation)
{
'OK'{
Backup-Database($dbs)
}
'Cancel'{
Write-Host "You clicked No"
}
}
}
)
上面的代码调用函数来备份数据库。此功能如下。
function Backup-Database ($dbnames)
{
foreach( $db in $dbnames )
{
Write-Host "[Now backing up $db]"
Backup-SqlDatabase -Database $db -BackupFile "S:\backup\$db.bak" -ServerInstance localhost
}
[System.Windows.MessageBox]::Show("`nDatabase Backup has completed.",'Tipper','OK','Info')
}
感谢您的阅读和帮助。
答案 0 :(得分:2)
在您的XAML代码中使用MultiDataTrigger
来定义条件,并在选中一个或多个复选框时删除IsEnabled="{Binding ElementName=UAT_DB1, Path=IsChecked}"
启用按钮:
<CheckBox Name="UAT_DB1" Content="UAT-DB1" HorizontalAlignment="Left" Margin="21,113,0,0" VerticalAlignment="Top" Cursor=""/>
<CheckBox Name="UAT_DB2" Content="UAT-DB2" HorizontalAlignment="Left" Margin="21,140,0,0" VerticalAlignment="Top"/>
<Button Name="btn_backup" Content="Backup" HorizontalAlignment="Left" Height="29" Margin="238,343,0,0" VerticalAlignment="Top" Width="78">
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ElementName=UAT_DB1, Path=IsChecked}" Value="False" />
<Condition Binding="{Binding ElementName=UAT_DB2, Path=IsChecked}" Value="False" />
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>