I have a powershell script that displays as a WPF form to allow users to control printers etc (map / unmap / set default), everything on it is working nicely, bit I have an issue with trying to hide a printer (from a work around where understandable listview hate having just 1 item bound to them)
So if the user has 1 mapped printer I also add in the "Send to OneNote 16" printer into the object that is bound to the list view, but to avoid confusion I'd ideally like to hide that printer from the list.
XAML Code section
<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>
Powershell bit that populates the object that is bound to MyListBox
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-MappedPrinterInfo
The style section all works a treat, but the datatrigger never fires, if I set the Setter property (4th line in XAML section) then I can hide/ unhide all items, but Ideally I want to be able to hide by name.
Thanks in advance.