Powershell XAML with a ListBox

24 views Asked by At

This my first Question ! In the following code in Powershell, I can't bind a collection of objects to a listBox.

The listbox remains empty I create a collection called dgSourceData in which usernames are stored. The XAML defines the window. The FormMain window is created and I add DataContext parameters. Then I create the binding to the ListBox. Finally, the window is displayed, but the user listbox remains empty.

using namespace System.Windows
using namespace System.Windows.Data
using namespace System.Windows.Controls
using namespace System.Windows.Markup
using namespace System.Xml
using namespace System.Collections.ObjectModel


Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase

# $Liste = Import-Csv .\usersAD.csv
$liste = "Marie","Michel","Jean","Adam","Paul"

$Out = @()
$dgSourceData = $Liste | ForEach-Object {
   $out += [PSCustomObject]@{
      Name = $_       # .Name
   }
   return $out
}
  
[xml]$xaml = @"
    <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:local="clr-namespace:SetTelephone"
        Title="MainWindow" Height="365" Width="837"
        >
    <Grid>
        <Label x:Name="LabelCompte" Content="Compte :" HorizontalAlignment="Left" Margin="122,128,0,0" VerticalAlignment="Top" Width="123" Height="31" FontSize="18" FontFamily="Arial"/>
        <Label x:Name="LabelTel" Content="Tel :" HorizontalAlignment="Left" Margin="453,128,0,0" VerticalAlignment="Top" FontFamily="Arial" FontSize="18" Width="80" Height="31"/>
        <TextBox x:Name="BoxTel" HorizontalAlignment="Left" Margin="552,128,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="139" FontFamily="Arial" FontSize="18" Height="31"/>
        <Button x:Name="BoutonMaj" Content="Maj" HorizontalAlignment="Left" Margin="376,255,0,0" VerticalAlignment="Top" Height="45" Width="100"/>
        <Button x:Name="BoutonLire" Content="Lire" HorizontalAlignment="Left" Margin="603,255,0,0" VerticalAlignment="Top" Height="43" Width="106"/>
        <ListBox x:Name="ListBox" Margin="215,127,389,162" FontFamily="Arial" FontSize="18"/> 
    </Grid>
</Window> 
"@

$FormMain = [XamlReader]::Load([XmlNodeReader] $xaml)

$FormMain.DataContext =
 @{
   # The collection must *directly* contain the objects to bind, hence the use of .SubObject
   GridDataSource = [ObservableCollection[Object]] $dgSourceData.SubObject
}

# Create a data binding.
$dgBinding = 
  [Binding] @{
    Mode = [BindingMode]::TwoWay
    Path = '[GridDataSource]' # Index into the hashtable stored in $FormMain.DataContext  
  }

$null = 
  [BindingOperations]::SetBinding(
    $FormMain.FindName('ListBox'), # the target grid control
    [DataGrid]::ItemsSourceProperty, # what grid property to bind to 
    $dgBinding # the data binding
  )

# $reader = new-object system.xml.xmlnodereader $xaml
# $Window = [windows.markup.xamlreader]::Load($reader)

 #
 # Gestions 
 # 

 # .......Button Management

 $Maj = $window.findName('BoutonMaj')
 $Lire = $window.findName('BoutonLire')


$null = $FormMain.ShowDialog()
0

There are 0 answers