Wpf 将变量绑定到Powershell中的combobox和textbox
我带着更多的问题来找你 我构建了一个简单的GUI来创建打印机或更改打印服务器上的驱动程序。但是,当我点击RUN按钮时,它不会使任何事情发生,只会弹出操作成功的消息。有人能帮我检查一下代码,告诉我我做错了什么吗Wpf 将变量绑定到Powershell中的combobox和textbox,wpf,powershell,xaml,user-interface,Wpf,Powershell,Xaml,User Interface,我带着更多的问题来找你 我构建了一个简单的GUI来创建打印机或更改打印服务器上的驱动程序。但是,当我点击RUN按钮时,它不会使任何事情发生,只会弹出操作成功的消息。有人能帮我检查一下代码,告诉我我做错了什么吗 #region pop up the gui [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void][System.Reflection.Assem
#region pop up the gui
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
Add-Type -AssemblyName PresentationFramework
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Print Queue Configuration Tool" Height="800" Width="800" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" Topmost="True">
<Grid>
<TextBlock HorizontalAlignment="Left" Margin="24,85,0,0" TextWrapping="Wrap" Text="Welcome to Print Queue Configuration Tool" VerticalAlignment="Top" Width="456" FontSize="22" FontWeight="Bold"/>
<Image HorizontalAlignment="Left" Height="41" Margin="537,36,0,0" VerticalAlignment="Top" Width="257" Source="logo.png"/>
<TextBlock HorizontalAlignment="Left" Margin="72,140,0,0" TextWrapping="Wrap" Text="What do you want to do?" VerticalAlignment="Top" RenderTransformOrigin="0.422,0.157" Height="31" Width="257" FontSize="16"/>
<RadioButton x:Name="RadioCreate" Content="Create a print queue." HorizontalAlignment="Left" Margin="102,194,0,0" VerticalAlignment="Top" IsChecked="True"/>
<RadioButton x:Name="RadioChange" Content="Change driver to a print queue." HorizontalAlignment="Left" Margin="311,194,0,0" VerticalAlignment="Top"/>
<ComboBox x:Name="ComboMaker" HorizontalAlignment="Left" Margin="460,260,0,0" VerticalAlignment="Top" Width="290">
<ComboBoxItem Content="Dascom"/>
<ComboBoxItem Content="HP"/>
<ComboBoxItem Content="Kyocera"/>
<ComboBoxItem Content="Lexmark"/>
<ComboBoxItem Content="Zebra"/>
</ComboBox>
<ComboBox x:Name="ComboModel" HorizontalAlignment="Left" Margin="460,310,0,0" VerticalAlignment="Top" Width="290">
</ComboBox>
<ComboBox x:Name="ComboTray" HorizontalAlignment="Left" Margin="460,360,0,0" VerticalAlignment="Top" Width="290">
</ComboBox>
<ComboBox x:Name="ComboDriver" HorizontalAlignment="Left" Margin="460,410,0,0" VerticalAlignment="Top" Width="290">
</ComboBox>
<TextBox x:Name="TextBoxPQ" HorizontalAlignment="Left" Height="22" Margin="460,460,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290" SelectionOpacity="4"/>
<TextBox x:Name="TextBoxServer" HorizontalAlignment="Left" Height="22" Margin="460,510,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290"/>
<TextBox x:Name="TextBoxHostname" HorizontalAlignment="Left" Height="22" Margin="460,560,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290"/>
<TextBox x:Name="TextBoxLocation" HorizontalAlignment="Left" Height="22" Margin="460,610,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="290"/>
<TextBlock HorizontalAlignment="Left" Margin="72,260,0,0" TextWrapping="Wrap" Text="Select the maker:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,310,0,0" TextWrapping="Wrap" Text="Select the model:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,360,0,0" TextWrapping="Wrap" Text="Select the no. of additional trays (only for model CX825, CX921, CX922, MX910, X950):" VerticalAlignment="Top" Width="304" Height="32" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,410,0,0" TextWrapping="Wrap" Text="Select the driver:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,460,0,0" TextWrapping="Wrap" Text="Add the print queue name:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,510,0,0" TextWrapping="Wrap" Text="Add the server name:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,560,0,0" TextWrapping="Wrap" Text="Add the hostname:" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Margin="72,610,0,0" TextWrapping="Wrap" Text="Add the location (i.e. DE-Eltmann):" VerticalAlignment="Top" Width="304" Height="22" FontWeight="Bold"/>
<Button x:Name="ButtonRun" Content="Run" HorizontalAlignment="Left" Margin="660,710,0,0" VerticalAlignment="Top" Width="90" Height="35"/>
</Grid>
</Window>
"@
#Read XAML
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
#Add actions to button Run
$RadioCreate = $window.FindName("RadioCreate")
$RadioChange = $window.FindName("RadioChange")
$ComboMaker = $window.FindName("ComboMaker")
$ComboModel = $window.FindName("ComboModel")
$ComboDriver = $window.FindName("ComboDriver")
$ComboMaker.Add_DropdownClosed({
$ComboModel.Items.Clear()
$ComboDriver.Items.Clear()
switch ($ComboMaker.Text) {
"Dascom" {
$ComboModel.Items.Add("Dascom T2150s")
$ComboModel.Items.Add("Dascom T2380")
}
"HP" {
$ComboModel.Items.Add("HP LaserJet M507dn")
$ComboModel.Items.Add("HP LaserJet M700 M712dn")
$ComboModel.Items.Add("HP Color LaserJet M553dn")
$ComboModel.Items.Add("HP OfficeJet Pro 8210")
$ComboModel.Items.Add("HP PageWide Pro 452dw")
$ComboModel.Items.Add("HP PageWide Enterprise Color 556dn")
$ComboModel.Items.Add("HP DesignJet T1600")
}
"Kyocera" {
$ComboModel.Items.Add("Kyocera P3155dn")
}
"Lexmark" {
$ComboModel.Items.Add("Lexmark CX625adhe")
$ComboModel.Items.Add("Lexmark CX825dte")
$ComboModel.Items.Add("Lexmark CX921de")
$ComboModel.Items.Add("Lexmark CX922de")
$ComboModel.Items.Add("Lexmark MX722adhe")
$ComboModel.Items.Add("Lexmark MS725dvn")
$ComboModel.Items.Add("Lexmark MX910de")
$ComboModel.Items.Add("Lexmark X950de")
}
"Zebra" {
$ComboModel.Items.Add("Zebra ZD620t 300 dpi")
$ComboModel.Items.Add("Zebra ZT620 300 dpi")
$ComboModel.Items.Add("Zebra 220Xi4 300 dpi")
$ComboModel.Items.Add("Zebra ZT610-300 dpi")
$ComboModel.Items.Add("Zebra ZQ630 Mobile Printer")
}
}
switch ($ComboMaker.Text) {
"Dascom" {
$ComboDriver.Items.Add("Tally T2150_")
}
"HP" {
$ComboDriver.Items.Add("HP DesignJet HPGL2 Driver")
$ComboDriver.Items.Add("HP Designjet T790ps 44in PS3")
$ComboDriver.Items.Add("HP DesignJet T930 HPGL2")
$ComboDriver.Items.Add("HP DesignJet Universal Print Driver HPGL2")
$ComboDriver.Items.Add("HP DesignJet Universal Print Driver HPGL2(v4.5.0)")
$ComboDriver.Items.Add("HP Universal Printing PS")
$ComboDriver.Items.Add("HP Universal Printing PS (v6.3.0)")
$ComboDriver.Items.Add("HP Universal Printing PS (v6.4.1)")
}
"Kyocera" {
$ComboDriver.Items.Add("Kyocera Classic Universaldriver KPDL")
$ComboDriver.Items.Add("Kyocera Classic Universaldriver KPDL (A4 models)")
}
"Lexmark" {
$ComboDriver.Items.Add("Lexmark Universal v2 PS3")
$ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.10.0.0)")
$ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.11.0.0)")
$ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.13.0.0)")
$ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.14.0.0)")
$ComboDriver.Items.Add("Lexmark Universal v2 PS3 (2.14.1.0)")
}
"Zebra" {
$ComboDriver.Items.Add("ZDesigner 110Xi4 203 dpi")
$ComboDriver.Items.Add("ZDesigner ZDesigner 110Xi4 300 dpi")
$ComboDriver.Items.Add("ZDesigner 220Xi4 300 dpi")
$ComboDriver.Items.Add("ZDesigner GX430t")
$ComboDriver.Items.Add("ZDesigner QLn420 (CPCL)")
$ComboDriver.Items.Add("ZDesigner QLn420 (ZPL)")
$ComboDriver.Items.Add("ZDesigner S4M-203dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZD620-300dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZM400 200 dpi (EPL)")
$ComboDriver.Items.Add("ZDesigner ZM400 200 dpi (ZPL)")
$ComboDriver.Items.Add("ZDesigner ZM400 300 dpi (ZPL)")
$ComboDriver.Items.Add("ZDesigner ZM600 200 dpi (ZPL)")
$ComboDriver.Items.Add("ZDesigner ZQ510 (ZPL)")
$ComboDriver.Items.Add("ZDesigner ZQ630 (ZPL)")
$ComboDriver.Items.Add("ZDesigner ZT230-200dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZT410-300dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZT420-300dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZT610-300dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZT610R-203dpi ZPL")
$ComboDriver.Items.Add("ZDesigner ZT610R-300dpi ZPL")
}
}
switch ($ComboModel.Text) {
"Lexmark CX825dte" {
$ComboTray.Items.Add("3")
}
"Lexmark CX921de" {
$ComboTray.Items.Add("0")
$ComboTray.Items.Add("1")
$ComboTray.Items.Add("2")
$ComboTray.Items.Add("3")
}
"Lexmark CX922de" {
$ComboTray.Items.Add("0")
$ComboTray.Items.Add("1")
$ComboTray.Items.Add("2")
$ComboTray.Items.Add("3")
}
"Lexmark MX910de" {
$ComboTray.Items.Add("0")
$ComboTray.Items.Add("1")
$ComboTray.Items.Add("2")
$ComboTray.Items.Add("3")
}
"Lexmark X950de" {
$ComboTray.Items.Add("0")
$ComboTray.Items.Add("1")
$ComboTray.Items.Add("2")
$ComboTray.Items.Add("3")
}
}
})
<# this part contains the variable assignment but not working
$maker = $ComboMaker.SelectedItem
$modelfull = $ComboModel.SelectedItem
$tray = $ComboTray.SelectedItem
$driver = $ComboDriver.SelectedItem
$TextBoxPQ = $window.FindName("TextBoxPQ")
$PQ = $TextBoxPQ.Text
$TextBoxServer = $window.FindName("TextBoxServer")
$server = $TextBoxServer.Text
$TextBoxHostname = $window.FindName("TextBoxHostname")
$hostname = $TextBoxHostname.Text
$TextBoxLocation = $window.FindName("TextBoxLocation")
$location = $TextBoxLocation.Text
#>
$ButtonRun = $window.FindName("ButtonRun")
$ButtonRun.Add_Click({
# here variables are not working as well
$maker = $ComboMaker.SelectedItem
$modelfull = $ComboModel.SelectedItem
$tray = $ComboTray.SelectedItem
$driver = $ComboDriver.SelectedItem
$TextBoxPQ = $window.FindName("TextBoxPQ")
$PQ = $TextBoxPQ.Text
$TextBoxServer = $window.FindName("TextBoxServer")
$server = $TextBoxServer.Text
$TextBoxHostname = $window.FindName("TextBoxHostname")
$hostname = $TextBoxHostname.Text
$TextBoxLocation = $window.FindName("TextBoxLocation")
$location = $TextBoxLocation.Text
if ($RadioCreate.Checked -eq $true){
PortCreation
PrinterCreation
if ($maker -eq 'Lexmark') {
ApplyConfigLexmark
}
}
if ($RadioChange.Checked -eq $true) {
ChangeDriver
if ($maker -eq 'Lexmark') {
ApplyConfigLexmark
}
}
[System.Windows.MessageBox]::Show('Operation completed!')
})
#Show form
$window.ShowDialog() | Out-Null
#endregion
我测试了你的代码,并修改了这行代码,以向制作人展示它的工作原理。为什么说变量没有绑定
[System.Windows.MessageBox]::Show('Operation completed!' + $maker)
此外,还应进行检查
if ($RadioCreate.IsChecked -eq $true){
检查我的更新答案,并让我知道是否有帮助或任何问题。当我按下运行按钮时,程序不会执行任何操作,甚至不会启动创建打印队列的过程。我已经挣扎了很长一段时间了,老实说,我不知道我还能做些什么来让它工作。它正在工作,但只有创建部分:)我会找出为什么其他部分不工作。现在我需要添加更多的MessageBox,以使其与用户更具交互性。非常感谢@很好,我的回答有用吗?
if ($RadioCreate.IsChecked -eq $true){