Home > Blockchain >  Get Value/Text from ListView and SubGridView - WPF Powershell
Get Value/Text from ListView and SubGridView - WPF Powershell

Time:07-13

I'm trying to create an app which gets network printers in our AD site and shows them into a ListView and a sub GridView (because it has columns with printer data) and the user has to select a printer to install. I'm having a problem to select the grid item and save it to a variable to parse it to the function which connects the network printer. I'm using Powershell runscape because I need the AD integration with windows printer cmdlets.

This is what I have by now (only the list view part and the powershell which fills the gridview:

<ListView Name="Impresoras" Margin="5" Grid.Row="0">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Laser" DisplayMemberBinding="{Binding PrinterName}" />
            <GridViewColumn Header="Driver" DisplayMemberBinding="{Binding DriverName}"/>
            <GridViewColumn Header="Ubicacion" DisplayMemberBinding="{Binding Location}"/>
            <GridViewColumn Header="IP" DisplayMemberBinding="{Binding PortName}"/>
            <GridViewColumn Header="Servidor" DisplayMemberBinding="{Binding ServerName}"/>
        </GridView>
    </ListView.View>
</ListView>

And this is the part where the Grid gets filled:

$tablaImpresoras = Get-ADObject -LDAPFilter "(objectCategory=printQueue)" -Properties PrinterName, DriverName, Location, PortName, ServerName | Select-Object -Property PrinterName, DriverName, Location, @{Name='PortName';Expression={$_.PortName}}, ServerName
$var_Impresoras.ItemsSource = $tablaImpresoras

I need a way to select an item from the grid and save it to a variable to run the function which connects the printer.

Thanks in advance for the help!

CodePudding user response:

You can pipe $var_Impresoras.SelectedItems to a ForEach-Object loop, and just reference $_.IP or whatever properties you need from the line. When I was last doing something similar that's what I did. If you have a function you're feeding this info to I'd personally setup the function to take info from the pipeline by property name, and then pipe $var_Impresoras.SelectedItems directly into the function. Something like:

Function Connect-Printer{
param(
    [parameter(ValueFromPipelinebyPropertyName=$True)]$IP,
    [parameter(ValueFromPipelinebyPropertyName=$True)]$Servidor,
    [parameter(ValueFromPipelinebyPropertyName=$True)]$Driver
)
    Some code to connect to the printer
}

$var_PrinterConnectButton.add_click({$var_Impresoras.SelectedItems | Connect-Printer})

Mind you the code in the function is pseudo code, but the parameter references should be fine to use as examples, and the last line should be solid reference material too.

CodePudding user response:

Thanks a lot! @TheMadTechnician Im using the .SelecteItems property with a Foreach-Objet as you sugested and it's working like a charm!

$cxnImpresora = $var_Impresoras.SelectedItems | Select-Object PrinterName, ServerName | ForEach-Object {"\\$($_.ServerName)\$($_.PrinterName)"}
$cxnImpresora = $cxnImpresora.ToString() 

This returns the ConnectionName for the printer I select in the list to use with the Add-Printer cmdlet.

  • Related