Get Value/Text from ListView and SubGridView - WPF Powershell - wpf

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!

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.

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

how to use file protocol open my documents

popToastControl.Payload =
$#"
<toast activationType='protocol' launch='file:///C|/My Documents/'>
<visual>
<binding template='ToastGeneric'>
<text>Click to launch Bing Maps</text>
<text>Searches for ""1 Microsoft Way, 98052""</text>
</binding>
</visual>
</toast>";
I cannot open my documents through file protocol(file:///...)
I tried like this: file:///%USERPROFILE%\My Documents
=>did not work
nor did this way: file:///%My Documents%
it only worked as this way :file:///C:\Users\Michael-DEV\Documents
Please get me off this trouble, much thanks!

Cannot find XAML elements as variables - Powershell 2.0

My script runs in Powershell V2.0 ISE but does not run in Powershell script. Why is this? I understand the variables cannot be null here, however I read these from the XAML and the button clicks do work. I get the following error:
You cannot call a method on a null-valued expression,
At C:\Program Files (x86)\Novertz\Main Script\Novertz.ps1 char:7
+ $FEL_CP6K01_button.add.click <<<< <<
+Category Info :InvalidOperation
I read the XAML with the following:
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$XamlPath
)
[xml]$Global:xmlWPF = Get-Content -Path $XamlPath
#Add WPF and Windows Forms assemblies
try{
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
} catch {
Throw "Failed to load Windows Presentation Framework assemblies."
}
#Create the XAML reader using a new XML node reader
$Global:xamGUI = [Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))
#Create hooks to each named object in the XAML
$xmlWPF.SelectNodes("//*[#Name]") | %{
Set-Variable -Name ($_.Name) -Value $xamGUI.FindName($_.Name) -Scope Global
}
And the powershell script:
#Required to load the XAML form and create the PowerShell Variables
cd "C:\Users\Leeds TX 12\Desktop\Novertz\GUI"
./LoadDialog.ps1 -XamlPath 'C:\Users\Leeds TX 12\Desktop\Novertz\GUI\myform.xaml'
$FEL_CP6K01_button.add_Click({#powershell script#})
$xamGUI.ShowDialog() | out-null
XAML:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta‌​tion" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="No-Vertz" Height="1080" Width="1920">
<Grid Margin="0,0,-233,-101"> <Grid.Effect> <DropShadowEffect/> </Grid.Effect>
<Button Name="FEL_CP6K01_button" Content="FEL_CP6K_CH01" HorizontalAlignment="Left" Margin="23,69,0,0" VerticalAlignment="Top" Width="99" Height="59"/>
Run powershell in single threaded apartment mode -STA

WPF String Formatting "String was not recognized as a valid DateTime."

I get this error when trying to have a WPF application try to format a string field as a datetime object:
<DataGridTextColumn Binding="{Binding Date, StringFormat={}{0:dd-MM-yyyy}}" Header="Date"/>
And the error I seem to be getting is:
"String was not recognized as a valid DateTime."
How do you fix this?
In my experience this error is caused by a lack of a set locale in the Window header, that is:
xml:lang="en-GB"
Or whatever is appropriate needs to be inserted into the Window header and that should fix the problem.

PieChart with Labels

I want to make LabeledPieCharts in WPF (Source: Bea's Blog )
I have downloaded the DemoProject and it runs. If I want to create a LabeledPieChart it fails with this Error:
The Tag "LabeledPieChart.Series" isn't in XML-Namespace "clr-namespace:Controls;assembly=Controls". Line 65 Position 14.
Here is my code:
<customControls:LabeledPieChart x:Name="labeledPieChart"
Title="Population of Puget Sound Cities"
Grid.Row="3"
Width="700"
Height="500"
BorderBrush="Gray">
<customControls:LabeledPieChart.Series>
<customControls:LabeledPieSeries x:Name="labeledPieSeries"
DependentValuePath="Population"
IndependentValuePath="Name"
IsSelectionEnabled="True"
ItemsSource="{Binding}"
LabelDisplayMode="Auto"
PieChartLabelItemTemplate="{StaticResource pieChartLabelDataTemplate}"
PieChartLabelStyle="{StaticResource pieChartLabelStyle}" />
</customControls:LabeledPieChart.Series>
</customControls:LabeledPieChart>
I've downloaded the project and your code seems to work OK. Maybe this question will help you a bit.

Silverlight 4 RIA datacontext error if I return less than 2 records

Hey there,
I am clueless in what is causing this but I get an error when my ria service if less than 2 items are been returned, everything works ok if I have 2 or more records been returned.
My domain data source looks like this:
<riaControls:DomainDataSource
AutoLoad="True"
x:Name="dsEmployee"
LoadedData="dsEmployee_LoadedData"
QueryName="GetEmployees">
<riaControls:DomainDataSource.DomainContext>
<my:TestDomainContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>
which is been bound to a listbox like this:
<ListBox x:Name="lstEmployees"
ItemsSource="{Binding ElementName=dsEmployee, Path=Data}"
However when I run the application I get this error:
System.ServiceModel.DomainServices.Client.DomainOperationException: Load operation failed for query 'GetEmployees'. Unexpected end of file. Following elements are not closed: IsEnabled, Employee, RootResults, GetEmployeesResult, GetEmployeesResponse. ---> System.Xml.XmlException: Unexpected end of file. Following elements are not closed: IsEnabled, Employee, RootResults, GetEmployeesResult, GetEmployeesResponse.
at System.ServiceModel.DomainServices.Client.WebDomainClient`1.EndQueryCore(IAsyncResult asyncResult)
at System.ServiceModel.DomainServices.Client.DomainClient.EndQuery(IAsyncResult asyncResult)
at System.ServiceModel.DomainServices.Client.DomainContext.CompleteLoad(IAsyncResult asyncResult)
--- End of inner exception stack trace ---
Has anyone had the same problem? am I doing something wrong here?
This looks like a problem with async server validation failing because it doesn't have enough time to complete. I don't think it is due to anything you did wrong.
There is some discussion of the issue here and some workarounds. Are you using System.Web, if so it sounds like that should not be used on client side code.
http://jeffhandley.com/archive/2010/05/26/asyncvalidation-again.aspx

Resources