WPF列表框上的值不可读/使用PowerShell

huangapple go评论60阅读模式
英文:

WPF value on listbox unreadable / using powershell

问题

I'm scripting on powershell and i use WPF on visual studio to had a script more userfriendly and i find it cool.

I finish my powershell script and my xml wpf too.

However when i get my value on my list box, the value are invisible, when my mouse is over the list box, the list box looks populated but this is unreadable.

WPF
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        ...
        <ListBox Name="Listbox_Host_Hyper_v" ...
        ...
        <ListBox Name="Listbox_VMs" ...
        ...
        <ListBox Name="Listbox_Storage_Path_Src" ...
        ...
        <ListBox Name="Listbox_Storage_Path_Dst" ...
        ...
        <ListBox Name="Listbox_Node_Hyper_v" ...
        ...
        ...
        <Button Name="Button_Show_Path" ...
    </Grid>
</Window>
Powershell
Add-Type -AssemblyName PresentationFramework
...
#Setup Button Show Path
$Window.FindName("Button_Show_Path").add_click({
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Selected_Node = (Get-ClusterNode -Cluster $Selected_Host).name
$global:Src_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -notlike ''*NB_*''}
$global:Dst_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -like ''*NB_*''}
Foreach ($element in $Dst_path){
$Window.FindName("Listbox_Storage_Path_Dst").Items.add($($element.Name)) | Out-Null
}
Foreach ($element in $Src_path){
$Window.FindName("Listbox_Storage_Path_Src").Items.add($($element.Name)) | Out-Null
}})
...

I try to resize on visual studio the different windows change many lines who are change on the actual code but can't understand why the value looks like invisible.

Thanks for your help.

Best regards.

A beginner

英文:

I'm scripting on powershell and i use WPF on visual studio to had a script more userfriendly and i find it cool.

I finish my powershell script and my xml wpf too.

However when i get my value on my list box, the value are invisible, when my mouse is over the list box, the list box looks populated but this is unreadble.

WPF
<Window 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:Migrate_Storage_CLU_Hyper_v"
mc:Ignorable="d"
Title="Migration Storage Cluster Hyper-v" Height="884" Width="820" WindowState="Normal" ResizeMode="CanResize" MinHeight="745" MinWidth="820" MaxHeight="755" MaxWidth="820">
<Grid Margin="0,0,0,-6">
<Grid.RowDefinitions>
<RowDefinition Height="722*"/>
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="161*"/>
</Grid.ColumnDefinitions>
<Label Name="Label_Cluster_Hyper_v" Content="Cluster Hyper-v :" HorizontalAlignment="Left" Margin="10,2,0,0" VerticalAlignment="Top" Width="185" Height="26" Grid.ColumnSpan="2"/>
<ListBox Name="Listbox_Host_Hyper_v" HorizontalAlignment="Left" Height="64" Margin="14,27,0,0" VerticalAlignment="Top" Width="296" Grid.ColumnSpan="2"/>
<Label Name="Label_VMs" Content="Virtuals Machines :" HorizontalAlignment="Left" Height="29" Margin="10,228,0,0" VerticalAlignment="Top" Width="149" Grid.ColumnSpan="2"/>
<ListBox Name="Listbox_VMs" HorizontalAlignment="Left" Height="445" Margin="14,267,0,0" VerticalAlignment="Top" Width="400" SelectionMode="Multiple" Grid.ColumnSpan="2"/>
<Label Name="Label_Storage_Path_Src" Content="Storage Path source :" HorizontalAlignment="Left" Height="29" Margin="10,96,0,0" VerticalAlignment="Top" Width="185" Grid.ColumnSpan="2"/>
<ListBox Name="Listbox_Storage_Path_Src" HorizontalAlignment="Left" Height="90" Margin="14,122,0,0" VerticalAlignment="Top" Width="343" SelectionMode="Multiple" Grid.ColumnSpan="2"/>
<Button Name="Button_Ok" Content="OK" Margin="558,685,134,1" Grid.Column="1" MinHeight="35" MinWidth="91" IsEnabled="False"/>
<Button Name="Button_Cancel" Content="Cancel" Margin="666,685,27,1" Grid.Column="1" MinHeight="35" MinWidth="91"/>
<ListBox Name="Listbox_Selected_VMs" HorizontalAlignment="Left" Height="379" Margin="446,267,0,0" VerticalAlignment="Top" Width="293" SelectionMode="Multiple" Grid.Column="1"/>
<Label Name="Label_Selected_VMs" Content="Selected Virtuals Machines :" HorizontalAlignment="Left" Height="29" Margin="442,233,0,0" VerticalAlignment="Top" Width="149" Grid.Column="1"/>
<Button Name="Button_Add" Grid.Column="1" Content=">" HorizontalAlignment="Left" Height="55" Margin="403,351,0,0" VerticalAlignment="Top" Width="37" FontWeight="Normal" FontSize="36" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
<Button Name="Button_Remove" Grid.Column="1" Content=">" HorizontalAlignment="Left" Height="55" Margin="403,431,0,0" VerticalAlignment="Top" Width="37" FontWeight="Normal" FontSize="36" RenderTransformOrigin="0.488,0.181" FlowDirection="RightToLeft"/>
<Button Name="Button_Check" Content="Check" Margin="454,685,240,1" Grid.Column="1" MinHeight="35" MinWidth="91"/>
<Button Name="Button_Display_VMs" Grid.Column="1" Content="Display VM" HorizontalAlignment="Left" Height="21" Margin="105,232,0,0" VerticalAlignment="Top" Width="93" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight" FontSize="10"/>
<ListBox Name="Listbox_Storage_Path_Dst" HorizontalAlignment="Left" Height="90" Margin="442,122,0,0" VerticalAlignment="Top" Width="343" SelectionMode="Multiple" Grid.Column="1"/>
<Label Name="Label_Storage_Path_Dst" Content="Storage Path destination :" HorizontalAlignment="Left" Height="29" Margin="439,96,0,0" VerticalAlignment="Top" Width="185" Grid.Column="1"/>
<ListBox Name="Listbox_Node_Hyper_v" HorizontalAlignment="Left" Height="64" Margin="445,27,0,0" VerticalAlignment="Top" Width="296" Grid.Column="1"/>
<Label Name="Label_Node_Hyper_v" Content=" Node Hyper-v :" HorizontalAlignment="Left" Margin="440,2,0,0" VerticalAlignment="Top" Width="185" Height="26" Grid.Column="1"/>
<Button Name="Button_Show_Node" Grid.Column="1" Content="Show Node" HorizontalAlignment="Left" Height="44" Margin="339,37,0,0" VerticalAlignment="Top" Width="66" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
<Button Name="Button_Show_Path" Grid.Column="1" Content="Show Path" HorizontalAlignment="Left" Height="44" Margin="359,142,0,0" VerticalAlignment="Top" Width="66" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
</Grid>
</Window>
Powershell
Add-Type -AssemblyName PresentationFramework
#########################################################################
###########################***___Function___***##########################
function Get-Check {
if (($Window.Findname("Listbox_Host_Hyper_v").SelectedItems -eq $null) -or ($Window.Findname("Listbox_Selected_VMs").Items -eq $null) -or ($Window.Findname("Listbox_Storage_Path").Items -eq $null) )
{
$Window.FindName("Label_EmptyField").Visibility ="Visible"
$Window.FindName("Button_Ok").IsEnabled = $False
$Window.FindName("Label_Resume").Visibility ="Hidden"
}
else 
{
$Window.FindName("Button_Ok").IsEnabled = $True
$Window.FindName("Label_EmptyField").Visibility ="Hidden"
$Window.FindName("Label_Resume").Visibility ="Visible"
$Window.FindName("Label_Resume").Content = "
Hôte Hyper-V = $Selected_Host
Chemin UNC = $Selected_UNCPATH
VM(s) sélectionné = $Selected_Virtuals
CSV = 
"
}
}
#########################################################################
#############################***___Variable___***#############################
#$global:Host_Hyper_v = Get-Cluster -Domain mydomain.lan | Where-object {$_.name -like "srv[1-9]*"}
#$global:Selected_UNCPATH = $($Window.Findname("Listbox_Storage_Path").SelectedItems)
#$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
#$global:Selected_Virtuals = $($Window.Findname("Listbox_Selected_VMs").SelectedItems)
#$global:Vm_On_Host = Get-VM -ClusterObject (Get-ClusterResource -Cluster $Selected_Host | where ResourceType -eq "Virtual Machine")
#$global:Dst_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -like '*NB_*'}
#$global:Src_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -notlike '*NB_*'}
#$global:Selected_Node = (Get-ClusterNode -Cluster $Selected_Host).name
#$global:Selected_Dst_Path = $($Window.Findname("Listbox_Storage_Path_Dst").SelectedItems)
#########################################################################
#############################***___XML___***#############################
[xml]$XML = @"
<Window 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="Migration Storage Cluster Hyper-v" Height="760" Width="820" WindowState="Maximized" ResizeMode="CanResize" MinHeight="745" MinWidth="820" MaxHeight="755" MaxWidth="820">
<Grid Margin="0,0,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="722*"/>
<RowDefinition Height="7*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="161*"/>
</Grid.ColumnDefinitions>
<Label Name="Label_Cluster_Hyper_v" Content="Cluster Hyper-v :" HorizontalAlignment="Left" Margin="10,2,0,0" VerticalAlignment="Top" Width="185" Height="26" Grid.ColumnSpan="2"/>
<ListBox Name="Listbox_Host_Hyper_v" HorizontalAlignment="Left" Height="64" Margin="14,27,0,0" VerticalAlignment="Top" Width="296" Grid.ColumnSpan="2"/>
<Label Name="Label_VMs" Content="Virtuals Machines :" HorizontalAlignment="Left" Height="29" Margin="10,228,0,0" VerticalAlignment="Top" Width="149" Grid.ColumnSpan="2"/>
<ListBox Name="Listbox_VMs" HorizontalAlignment="Left" Height="445" Margin="14,267,0,0" VerticalAlignment="Top" Width="400" SelectionMode="Multiple" Grid.ColumnSpan="2"/>
<Label Name="Label_Storage_Path_Src" Content="Storage Path source :" HorizontalAlignment="Left" Height="29" Margin="10,96,0,0" VerticalAlignment="Top" Width="185" Grid.ColumnSpan="2"/>
<ListBox Name="Listbox_Storage_Path_Src" HorizontalAlignment="Left" Height="90" Margin="14,122,0,0" VerticalAlignment="Top" Width="343" SelectionMode="Multiple" Grid.ColumnSpan="2"/>
<Button Name="Button_Ok" Content="OK" Margin="558,685,134,1" Grid.Column="1" MinHeight="35" MinWidth="91" IsEnabled="False"/>
<Button Name="Button_Cancel" Content="Cancel" Margin="666,685,27,1" Grid.Column="1" MinHeight="35" MinWidth="91"/>
<ListBox Name="Listbox_Selected_VMs" HorizontalAlignment="Left" Height="379" Margin="446,267,0,0" VerticalAlignment="Top" Width="293" SelectionMode="Multiple" Grid.Column="1"/>
<Label Name="Label_Selected_VMs" Content="Selected Virtuals Machines :" HorizontalAlignment="Left" Height="29" Margin="442,233,0,0" VerticalAlignment="Top" Width="149" Grid.Column="1"/>
<Button Name="Button_Add" Grid.Column="1" Content=">" HorizontalAlignment="Left" Height="55" Margin="403,351,0,0" VerticalAlignment="Top" Width="37" FontWeight="Normal" FontSize="36" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
<Button Name="Button_Remove" Grid.Column="1" Content=">" HorizontalAlignment="Left" Height="55" Margin="403,431,0,0" VerticalAlignment="Top" Width="37" FontWeight="Normal" FontSize="36" RenderTransformOrigin="0.488,0.181" FlowDirection="RightToLeft"/>
<Button Name="Button_Check" Content="Check" Margin="454,685,240,1" Grid.Column="1" MinHeight="35" MinWidth="91"/>
<Button Name="Button_Display_VMs" Grid.Column="1" Content="Display VM" HorizontalAlignment="Left" Height="21" Margin="105,232,0,0" VerticalAlignment="Top" Width="93" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight" FontSize="10"/>
<ListBox Name="Listbox_Storage_Path_Dst" HorizontalAlignment="Left" Height="90" Margin="442,122,0,0" VerticalAlignment="Top" Width="343" SelectionMode="Multiple" Grid.Column="1"/>
<Label Name="Label_Storage_Path_Dst" Content="Storage Path destination :" HorizontalAlignment="Left" Height="29" Margin="439,96,0,0" VerticalAlignment="Top" Width="185" Grid.Column="1"/>
<ListBox Name="Listbox_Node_Hyper_v" HorizontalAlignment="Left" Height="64" Margin="445,27,0,0" VerticalAlignment="Top" Width="296" Grid.Column="1"/>
<Label Name="Label_Node_Hyper_v" Content=" Node Hyper-v :" HorizontalAlignment="Left" Margin="440,2,0,0" VerticalAlignment="Top" Width="185" Height="26" Grid.Column="1"/>
<Button Name="Button_Show_Node" Grid.Column="1" Content="Show Node" HorizontalAlignment="Left" Height="44" Margin="339,37,0,0" VerticalAlignment="Top" Width="66" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
<Button Name="Button_Show_Path" Grid.Column="1" Content="Show Path" HorizontalAlignment="Left" Height="44" Margin="359,142,0,0" VerticalAlignment="Top" Width="66" FontWeight="Normal" RenderTransformOrigin="0.488,0.181" FlowDirection="LeftToRight"/>
</Grid>
</Window>
"@
$FormXML = (New-Object System.Xml.XmlNodeReader $XML)
$Window = [Windows.Markup.XamlReader]::Load($FormXML)
#########################################################################
###########################***___Button___***##########################
#Setup Check button
$Window.FindName("Button_Check").add_click({
Get-Check         
})
#Setup Cancel button 
$Window.FindName("Button_Cancel").add_click({
$Window.Close()
})
#Setup List Cluster Hyper-V 
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Host_Hyper_v = Get-Cluster -Domain mydomain.lan | Where-object {$_.name -like "srv[1-9]*"}
Foreach ($element in $Host_Hyper_v){
$Window.FindName("Listbox_Host_Hyper_v").Items.add($($element.Name)) | Out-Null
}
#Setup List Node Hyper-V 
#Setup List UNC Path source
#Foreach ($element in $Src_Path){
#$Window.FindName("Listbox_Storage_Path").Items.add($($element.Name)) | Out-Null
#}
#Setup List UNC Path destination
Foreach ($element in $Dst_Path){
$Window.FindName("Listbox_Storage_Path_Dst").Items.add($($element.Name)) | Out-Null
}
#Setup Button Display VM
$Window.FindName("Button_Display_VMs").add_click({
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Vm_On_Host = Get-VM -ClusterObject (Get-ClusterResource -Cluster $Selected_Host | where ResourceType -eq "Virtual Machine")
Foreach ($element in $VM_On_Host){
$Window.FindName("Listbox_VMs").Items.add($($element.Name)) | Out-Null
}})
#Setup Button Show Node
$Window.FindName("Button_Show_Node").add_click({
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Selected_Node = (Get-ClusterNode -Cluster $Selected_Host).name
Foreach ($element in $Selected_Node){
$Window.FindName("Listbox_Node_Hyper_v").Items.add($($element.Name)) | Out-Null
}})
#Setup Button Show Path
$Window.FindName("Button_Show_Path").add_click({
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Selected_Node = (Get-ClusterNode -Cluster $Selected_Host).name
$global:Src_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -notlike '*NB_*'}
$global:Dst_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -like '*NB_*'}
Foreach ($element in $Dst_path){
$Window.FindName("Listbox_Storage_Path_Dst").Items.add($($element.Name)) | Out-Null
}
Foreach ($element in $Src_path){
$Window.FindName("Listbox_Storage_Path_Src").Items.add($($element.Name)) | Out-Null
}})
#Setup Button Add
$Window.FindName("Button_Add").add_click({
$VMs = $($Window.FindName("Listbox_VMs").SelectedItems)
Foreach ($element in $VMs){
$Window.FindName("Listbox_SelectedVMs").Items.add($element) | Out-Null
$Window.FindName("Listbox_VMs").Items.remove($element) | Out-Null
$Window.UpdateLayout() 
}
})
#Setup Button Remove
$Window.FindName("Button_Remove").add_click({
$Server = $($Window.FindName("Listbox_SelectedVMs").SelectedItems)
Foreach ($element in $Server){
$Window.FindName("Listbox_VMs").Items.add($element) | Out-Null
$Window.FindName("Listbox_SelectedVMs").Items.remove($element) | Out-Null
$Window.UpdateLayout() 
}
})
#Setup OK button
$Window.FindName("Button_OK").add_click({
$global:Selected_Host = $($Window.Findname("Listbox_Host_Hyper_v").SelectedItems)
$global:Selected_Virtuals = $($Window.Findname("Listbox_Selected_VMs").SelectedItems)
$global:Selected_Node = (Get-ClusterNode -Cluster $Selected_Host).name
$global:Dst_path = ((Get-ClusterSharedVolume -Cluster $Selected_Host).SharedVolumeInfo).FriendlyVolumeName | Where-object {$_ -like '*PS_*'}
foreach ($element in $Selected_Virtuals){
Move-VMStorage `
ComputerName $element `
DestinationStoragePath $Selected_Dst_Path`
Name $element
}
})
#########################################################################
#########################***___OPEN-WINDOWS___***########################
$Window.ShowDialog() | Out-Null

I try to resize on visual studio the differents windows change many lines who are change on the actually code but can't understand why the value looks like invisible.

Thnks for your help.

Best regards.

A beginner

答案1

得分: 0

尝试将ListBoxItemsSource设置为以下方式:

$items = (Get-Cluster ...
$listBox.ItemsSource = $items
英文:

Try to set set the ItemsSource of the ListBox something like this:

$items = (Get-Cluster ...
$listBox.ItemsSource = $items

huangapple
  • 本文由 发表于 2023年6月1日 18:21:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定