使用WPF的Powershell ListView - 项目显示不正确

时间:2018-02-14 20:46:47

标签: wpf powershell listview listviewitem

我试图将以下查询的结果输出到两列:

get-mailbox -identity *@$SearchDomain | where ismailboxenabled -eq true

以下是我现在所做的事情:

$mailboxes = get-mailbox -identity *@$SearchDomain | where ismailboxenabled -eq true

foreach($line in $mailboxes){
    $new = new-object System.Windows.Forms.ListViewItem($line.Name)
    $new.SubItems.Add($line.Alias + "@" + $SearchDomain)
    $WPFLstActiveMailboxes.Items.Add($new)
}

我的问题是我的GUI中出现了这样的事情:

GUI

黑框后面是我想要的数据

编辑1:

    $inputXML = @"
<Window x:Name="TMS_MailboxToolkit" x:Class="TMS_MailboxToolki.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TMS"
        mc:Ignorable="d"
        Title="TMS - Mailbox Toolkit" Height="354.303" Width="527.152">
    <Grid Margin="0,0,-8,-5">
        <Button x:Name="BtnCalculate" Content="Calculate" HorizontalAlignment="Left" Margin="432,289,0,0" VerticalAlignment="Top" Width="75"/>
        <Label x:Name="LblSearchDomain" Content="Search domain" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        <TextBox x:Name="TxtSearchDomain" HorizontalAlignment="Left" Height="23" Margin="10,42,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="malicis.com"/>

        <Label x:Name="LblActiveMailboxes" Content="Active mailboxes" HorizontalAlignment="Left" Margin="10,74,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        <Label x:Name="LblCurrentCount" Content="Current count:" HorizontalAlignment="Left" Margin="10,286,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="TxtCurrentCount" HorizontalAlignment="Left" Height="23" Margin="101,290,0,0" TextWrapping="Wrap" Text="00000" VerticalAlignment="Top" Width="45" IsEnabled="False" FontWeight="Bold"/>
        <ProgressBar x:Name="PrgStatus" HorizontalAlignment="Left" Height="10" Margin="345,70,0,0" VerticalAlignment="Top" Width="162"/>
        <TextBox x:Name="TxtStatus" HorizontalAlignment="Left" Height="23" Margin="345,42,0,0" TextWrapping="Wrap" IsEnabled="False" Text="idle" VerticalAlignment="Top" Width="162" FontStyle="Italic"/>
        <Label x:Name="LblStatus" Content="Status" HorizontalAlignment="Left" Margin="345,10,0,0" VerticalAlignment="Top" FontWeight="Bold"/>
        <ListView x:Name="LstActiveMailboxes" HorizontalAlignment="Left" Height="181" Margin="10,100,0,0" VerticalAlignment="Top" Width="497">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Disable">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Margin="5, 0" IsChecked="False"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn >

                    <GridViewColumn Header="Name"/>
                    <GridViewColumn Header="Alias"/>
                    <GridViewColumn Header="Mailbox"/>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</Window>
"@       

$inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N'  -replace '^<Win.*', '<Window'

[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[xml]$XAML = $inputXML
#Read XAML

    $reader=(New-Object System.Xml.XmlNodeReader $xaml)
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged properties (PowerShell cannot process them)"
    throw}

#===========================================================================
# Load XAML Objects In PowerShell
#===========================================================================

$xaml.SelectNodes("//*[@Name]") | %{"trying item $($_.Name)";
    try {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop}
    catch{throw}
    }

Function Get-FormVariables{
if ($global:ReadmeDisplay -ne $true){Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true}
write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
get-variable WPF*
}

Get-FormVariables

#===========================================================================
# Shows the form
#===========================================================================
Add-Type -AssemblyName System.Windows.Forms
Set-Variable -Name credsalreadyprovided -Value $false -Scope global
Set-Variable -Name CONFIG_SERVER -Value "exchpd01" -Scope global

$WPFBtnCalculate.Add_Click({
if ($credsalreadyprovided -eq $true){
    calculate
}
else{
    initiatesession
    calculate
}
})

function initiatesession{
    $WPFPrgStatus.Value = 0
    Set-Variable -Name cred -Value (get-credential) -Scope global
    $credsalreadyprovided = $true
    $WPFPrgStatus.Value = 25
    $WPFTxtStatus.Text = "Establishing session with " + $CONFIG_SERVER  + "..."
    Set-Variable -Name session -Value (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$CONFIG_SERVER/PowerShell/ -credential $cred) -Scope global
    $WPFPrgStatus.Value = 50
    $WPFTxtStatus.Text = "Importing session..."
    Import-PSSession $session -AllowClobber
}

function calculate{
$WPFPrgStatus.Value = 100
$WPFTxtStatus.Text = "Querying " + $CONFIG_SERVER + "..."
$SearchDomain=$WPFTxtSearchDomain.Text
$WPFTxtCurrentCount.Text = (get-Mailbox -identity *@$SearchDomain | where ismailboxenabled -eq true).count

#$mailboxes = get-mailbox -identity *@$SearchDomain | where ismailboxenabled -eq true


get-mailbox -identity *@$SearchDomain | where ismailboxenabled -eq true | ForEach-Object{
    $name = $_.Name
    $entry = New-Object System.Windows.Controls.ListViewItem($name)
    $alias = $_.Alias
    $entry.SubItems.Add($alias)
    $mailbox = $_.Alias + "@" + $SearchDomain
    $entry.SubItems.Add($mailbox)
    $WPFLstActiveMailboxes.Items.Add($entry)
}
}

$Form.ShowDialog() | out-null

1 个答案:

答案 0 :(得分:0)

我一直在做的是在WPF中使用数据绑定,以便它绑定到我想要的属性,然后只创建一个包含我在WPF中绑定的属性的psobject,然后将它们添加到ListView中。物品收藏。

这是wpf设置的参考,然后确保你的psobject属性与wpf中的绑定属性完全相同

DataGridView Column binding in WPF