我有一个powershell脚本,它以WPF表单的形式显示,允许用户控制打印机等(map / unmap / set默认设置),它上的一切都运行得很好,我想隐藏打印机有一个问题(从一个可以理解的列表视图讨厌只有一个项目绑定到他们的工作)
因此,如果用户有1台映射打印机,我也会将"Send 16“打印机添加到绑定到list视图的对象中,但为了避免混淆,我希望将打印机隐藏在列表中。
XAML代码部分
<ListBox x:Name="MyListbox" HorizontalContentAlignment="Stretch">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="ListBoxItem.Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Name}" Value="Send to OneNote 16">
<Setter Property="ListBoxItem.Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Border CornerRadius="6" BorderBrush="LightGray" Background="White" BorderThickness="1" Padding="8">
<StackPanel>
<DockPanel>
<TextBlock Text="Name: " FontWeight="Bold" Foreground="LimeGreen" />
<TextBlock>
<TextBlock FontWeight="Bold" Text="{Binding Path=Name}" />
</TextBlock>
</DockPanel>
<DockPanel>
<TextBlock Text="Server: " />
<TextBlock Foreground="Black">
<TextBlock Text="$($PrintServerName)" />
</TextBlock>
<TextBlock Text=" | Job Count: " />
<TextBlock Foreground="Black">
<TextBlock Text="{Binding Path=JobCount}" />
</TextBlock>
</DockPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>填充绑定到MyListBox的对象的Powershell位
function Get-MappedPrinterInfo {
BEGIN {
# Setup variables, and get mapped printer information from registry
[object]$Printers = Get-ChildItem Registry::\HKEY_CURRENT_USER\Printers\Connections
[object]$Output = New-Object System.Collections.ObjectModel.ObservableCollection[Object]
}
PROCESS {
foreach ($Printer in $Printers) {
# Extract printer name
[string]$PrinterName = $Printer.Name
# Correct Case
$PrinterName = $PrinterName.ToLower()
# Cleanup data
$PrinterName = $PrinterName.replace("printerserver.mynetwork.local", "")
$PrinterName = $PrinterName.replace("printerserver", "")
$PrinterName = $PrinterName.replace("hkey_current_user\printers\connections\", "")
$PrinterName = $PrinterName.replace(",", "")
# Get additional data on printer from its print server
$Output += Get-Printer -ComputerName $PrintServerName | Where-Object Shared -eq $true | Where-Object Name -eq $PrinterName
}
# Check object length, and pad out if required.
if ($Output.Count -eq 1) {
$Output += Get-Printer | Where-Object Name -eq "Send to OneNote 16"
}
}
END {
# Return data
return $Output
}
}
# Populate my mapped printer list.
$MyListbox.ItemsSource = Get-MappedPrinterInfostyle部分都可以处理,但是数据处理程序从不触发,如果我设置Setter属性( XAML部分的第4行),那么我可以隐藏/取消隐藏所有项,但理想情况下,我希望能够按名称隐藏。
提前谢谢。
发布于 2020-01-10 10:39:16
你没有必要增加额外的项目-然后隐藏-它解决了:
问题的根源在于,从PowerShell函数输出集合会导致默认的枚举,在单个项集合的情况下,这意味着该项本身是输出的,而不是项数组([object[]],这是PowerShell在分配给l-值时从命令中隐式收集多个输出的方式)。
@() ( 数组子表达式运算符 )正是为了解决这种情况而存在的:
将其封装在一个命令的周围,即使该命令碰巧输出了单个对象,但它的输出始终希望被视为数组:
# Populate my mapped printer list.
# @(...) ensures that the function output is an array.
$MyListbox.ItemsSource = @(Get-MappedPrinterInfo)请注意,可以从函数内部解决这个问题--通过将输出集合包装在aux中。单项数组,以便调用方接收原始集合,但通常最好不要编写此类函数,因为它们在管道中使用时会表现出非标准行为,在管道中,其他命令期望逐项输入,而不是整个集合。
如果您仍然想这样做(这对于私人的、特殊的功能来说是可以的):
...
END {
# Return data wrapped in a single-item aux. array to ensure
# that the $Output collection is returned as a whole.
return , $Output
}有关您的代码的几个方面:
[object]$Printers = ...
[object]强制转换在PowerShell中是没有意义的,而强制转换通常不像在C#中那样工作。
无论是何种类型的$Printers,都成为了有效的RHS类型。
但是,如果您使用特定类型约束LHS,则无论何时向该变量赋值,都会尝试转换到该类型。
$Output += ...
+=悄悄地将$Output转换为(新实例的一个)数组([object[]]),而不管原始类型如何,这不是您的意图,也是效率低下的。
您需要在最初存储在.Add()中的集合实例上调用$Output方法。
https://stackoverflow.com/questions/59678493
复制相似问题