none
XAML ListBox mit Checkboxen RRS feed

  • Frage

  • Hallo,

     

    ich versuche mithilfe von XAML eine ListBox inklusive Checkboxen zu erstellen. Der Input dafür wird aus einer XML Datei eingelesen. Leider besitzen die Checkboxen keinen Anzeigennamen....

    Ich bin über jede Hilfe dankbar :)

    xaml gui.ps1

    #Build the GUI
    [xml]$xaml = @"
    <Window 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Name="Window" Title="install Software" WindowStartupLocation="CenterScreen" Height="500" Width="550" ShowInTaskbar="True" Background="lightgray"> 
      <StackPanel>
        <StackPanel Orientation="Horizontal">  
          <Label Content="Suchen:" Height="30" Width="50" Margin="10,20,10,10"/>
          <TextBox x:Name="inputTextBox" Height="22" Width ="460"/>
        </StackPanel>
        <StackPanel  Orientation="Horizontal">   
          <Button x:Name="addButton" Content="Hinzufügen" Height="25" Width="255" Margin="10,0,5,10" Background="white"/>
          <Button x:Name="removeButton" Content="Entfernen" Height="25" Width="255" Margin="5,0,5,10" Background="white"/>
        </StackPanel>    
        <ListBox x:Name="listbox" SelectionMode="Extended" Margin="10,5,10,5">
          <ListBox.ItemTemplate>
            <DataTemplate>
               <CheckBox x:Name="check" Content="{Binding Name}" IsChecked="{Binding IsChecked}" Margin="2,2,0,0"/>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
        <Button x:Name="install" Content="Installieren" Height="25" Margin="5,5,10,5" Background="white"/>
      </StackPanel>
    </Window>
    "@
     
    $reader=(New-Object System.Xml.XmlNodeReader $xaml)
    $Window=[Windows.Markup.XamlReader]::Load( $reader )
     
    #Connect to Controls
    $inputTextBox = $Window.FindName('inputTextBox')
    $addButton = $Window.FindName('addButton')
    $listbox = $Window.FindName('listbox')
    $removeButton = $Window.FindName('removeButton')
    
    #readXML
    [xml]$list = Get-Content -Path "$PSScriptRoot\config.xml"
    foreach( $config in $list.config.software)
    {
      $listbox.Items.Add( [System.String[]](@(" $($config.package)")))
    }
    
    #Events
    $addButton.Add_Click({
        If ((-NOT [string]::IsNullOrEmpty($inputTextBox.text))) {
            $listbox.Items.Add($inputTextBox.text)
            $inputTextBox.Clear()
        }
    })
    $removeButton.Add_Click({
        While ($listbox.SelectedItems.count -gt 0) {
            $listbox.Items.RemoveAt($listbox.SelectedIndex)
        }
    })
    $listbox.Add_Drop({
        (Get-Content $_.Data.GetFileDropList()) | ForEach {
            $listbox.Items.Add($_)
        }
    })
    
    $Window.ShowDialog() | Out-Null

    config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
      <software package="Firefox"></software>
      <software package="AdobeReader"></software>
      <software package="Gimp"></software>
      <software package="XMind"></software>
      <software package="Google Chrome"></software>
      <software package="Notepad++"></software>
      <software package="7zip"></software>
      <software package="LibreOffice"></software>
      <software package="Xampp"></software>
    </config>


    Freitag, 31. Juli 2015 14:40

Antworten

  • Moin,

    #Build the GUI
    [xml]$xaml = @"
    <Window 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Name="Window" Title="install Software" WindowStartupLocation="CenterScreen" Height="500" Width="550" ShowInTaskbar="True" Background="lightgray"> 
      <StackPanel>
        <StackPanel Orientation="Horizontal">  
          <Label Content="Suchen:" Height="30" Width="50" Margin="10,20,10,10"/>
          <TextBox x:Name="inputTextBox" Height="22" Width ="460"/>
        </StackPanel>
        <StackPanel  Orientation="Horizontal">   
          <Button x:Name="addButton" Content="Hinzufügen" Height="25" Width="255" Margin="10,0,5,10" Background="white"/>
          <Button x:Name="removeButton" Content="Entfernen" Height="25" Width="255" Margin="5,0,5,10" Background="white"/>
        </StackPanel>    
        
        <ListBox x:Name="listbox" SelectionMode="Extended" Margin="10,5,10,5">
          <ListBox.ItemTemplate>
            <DataTemplate>
               <CheckBox x:Name="check" Content="{Binding}" IsChecked="{Binding IsChecked}" Margin="2,2,0,0"/>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
        
        <Button x:Name="install" Content="Installieren" Height="25" Margin="5,5,10,5" Background="white"/>
      </StackPanel>
    </Window>
    "@
     
    $reader=(New-Object System.Xml.XmlNodeReader $xaml)
    $Window=[Windows.Markup.XamlReader]::Load( $reader )
     
    #Connect to Controls
    $inputTextBox = $Window.FindName('inputTextBox')
    $addButton = $Window.FindName('addButton')
    $listbox = $Window.FindName('listbox')
    $removeButton = $Window.FindName('removeButton')
    
    #readXML
    [xml]$list = Get-Content -Path "$PSScriptRoot\config.xml"
    foreach( $config in $list.config.software)
    {
      $package = $config.package
      $listbox.Items.Add("$package") | Out-Null
    }
    
    #Events
    $addButton.Add_Click({
        If ((-NOT [string]::IsNullOrEmpty($inputTextBox.text))) {
            $listbox.Items.Add($inputTextBox.text)
            $inputTextBox.Clear()
        }
    })
    $removeButton.Add_Click({
        While ($listbox.SelectedItems.count -gt 0) {
            $listbox.Items.RemoveAt($listbox.SelectedIndex)
        }
    })
    $listbox.Add_Drop({
        (Get-Content $_.Data.GetFileDropList()) | ForEach {
            $listbox.Items.Add($_)
        }
    })
    
    $Window.ShowDialog() | Out-Null

    Bei der Zuweisung vom Content der Checkbox reicht einfach nur 

    Content="{Binding}"

    Deine Zeile hat ein Array zurückgegeben, das hab ich durch folgendes ersetzt

    $package = $config.package
    $listbox.Items.Add("$package") | Out-Null

    Gruß,

    • Als Antwort markiert ILoveHorses Dienstag, 4. August 2015 13:27
    Montag, 3. August 2015 16:01

Alle Antworten

  • Moin,

    #Build the GUI
    [xml]$xaml = @"
    <Window 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Name="Window" Title="install Software" WindowStartupLocation="CenterScreen" Height="500" Width="550" ShowInTaskbar="True" Background="lightgray"> 
      <StackPanel>
        <StackPanel Orientation="Horizontal">  
          <Label Content="Suchen:" Height="30" Width="50" Margin="10,20,10,10"/>
          <TextBox x:Name="inputTextBox" Height="22" Width ="460"/>
        </StackPanel>
        <StackPanel  Orientation="Horizontal">   
          <Button x:Name="addButton" Content="Hinzufügen" Height="25" Width="255" Margin="10,0,5,10" Background="white"/>
          <Button x:Name="removeButton" Content="Entfernen" Height="25" Width="255" Margin="5,0,5,10" Background="white"/>
        </StackPanel>    
        
        <ListBox x:Name="listbox" SelectionMode="Extended" Margin="10,5,10,5">
          <ListBox.ItemTemplate>
            <DataTemplate>
               <CheckBox x:Name="check" Content="{Binding}" IsChecked="{Binding IsChecked}" Margin="2,2,0,0"/>
            </DataTemplate>
          </ListBox.ItemTemplate>
        </ListBox>
        
        <Button x:Name="install" Content="Installieren" Height="25" Margin="5,5,10,5" Background="white"/>
      </StackPanel>
    </Window>
    "@
     
    $reader=(New-Object System.Xml.XmlNodeReader $xaml)
    $Window=[Windows.Markup.XamlReader]::Load( $reader )
     
    #Connect to Controls
    $inputTextBox = $Window.FindName('inputTextBox')
    $addButton = $Window.FindName('addButton')
    $listbox = $Window.FindName('listbox')
    $removeButton = $Window.FindName('removeButton')
    
    #readXML
    [xml]$list = Get-Content -Path "$PSScriptRoot\config.xml"
    foreach( $config in $list.config.software)
    {
      $package = $config.package
      $listbox.Items.Add("$package") | Out-Null
    }
    
    #Events
    $addButton.Add_Click({
        If ((-NOT [string]::IsNullOrEmpty($inputTextBox.text))) {
            $listbox.Items.Add($inputTextBox.text)
            $inputTextBox.Clear()
        }
    })
    $removeButton.Add_Click({
        While ($listbox.SelectedItems.count -gt 0) {
            $listbox.Items.RemoveAt($listbox.SelectedIndex)
        }
    })
    $listbox.Add_Drop({
        (Get-Content $_.Data.GetFileDropList()) | ForEach {
            $listbox.Items.Add($_)
        }
    })
    
    $Window.ShowDialog() | Out-Null

    Bei der Zuweisung vom Content der Checkbox reicht einfach nur 

    Content="{Binding}"

    Deine Zeile hat ein Array zurückgegeben, das hab ich durch folgendes ersetzt

    $package = $config.package
    $listbox.Items.Add("$package") | Out-Null

    Gruß,

    • Als Antwort markiert ILoveHorses Dienstag, 4. August 2015 13:27
    Montag, 3. August 2015 16:01
  • Danke hat alles geklappt :)
    Dienstag, 4. August 2015 13:22