Advertisement
Guest User

Powershell WPF DataTemplate Button_click

a guest
Mar 23rd, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
  2. [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  3. #Region Build the MAINWindow
  4.  
  5. [xml]$xamlMain = @"
  6. <Window
  7.        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  8.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  9.        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  10.        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  11.        xmlns:sys="clr-namespace:System;assembly=mscorlib"
  12.        xmlns:local="clr-namespace:WW_Import"
  13.        xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
  14.    x:Name="Window" Title="MainWindow"  Height="350" Width="525">
  15.    <Grid>
  16.        <Button x:Name="btnTest" Content="Button" HorizontalAlignment="Left" Margin="202,223,0,0" VerticalAlignment="Top" Width="75"/>
  17.        <ListBox x:Name="listBox" HorizontalAlignment="Left" Height="105" Margin="30,40,0,0" VerticalAlignment="Top" Width="429">
  18.            <ListBox.ItemTemplate>
  19.                <DataTemplate>
  20.                    <Grid Margin="0,2">
  21.                        <Grid.ColumnDefinitions>
  22.                            <ColumnDefinition Width="190" />
  23.                            <ColumnDefinition Width="*" />
  24.                        </Grid.ColumnDefinitions>
  25.                        <Button  x:Name="btnRemoveOSCollEntry" Content="X"/>
  26.                        <TextBlock Margin="10,0,0,0" Grid.Column="2" Text="{Binding Title}" />
  27.                    </Grid>
  28.                </DataTemplate>
  29.            </ListBox.ItemTemplate>
  30.        </ListBox>
  31.         <Label x:Name="label" Content="Label" HorizontalAlignment="Left" Margin="147,164,0,0" VerticalAlignment="Top" Width="247"/>
  32.    </Grid>
  33. </Window>
  34. "@
  35. #endregion
  36.  
  37. $global:CurrentLocation = Get-Location
  38.  
  39. function SetMainWindow($Picture, $HeaderText, $Text)
  40. {
  41.     #region Controls
  42.     $tMain = $WindowMain.FindName("tMain")
  43.     $tHeader = $WindowMain.FindName("tHeader")
  44.     $iMain = $WindowMain.FindName("iMain")
  45.     $MainBorder = $WindowMain.FindName("MainBorder")
  46.     #endregion
  47.    
  48.     $WindowMain.FindName("MainBackground").Background = "#FF076A80"
  49.    
  50.     $ScreenRes = [System.Windows.SystemParameters]::PrimaryScreenWidth
  51.     if ($ScreenRes)
  52.     {
  53.         $WindowMain.Left = $Screenres - $WindowMain.Width
  54.     }
  55.    
  56. }
  57.  
  58. $readerMain = (New-Object System.Xml.XmlNodeReader $xamlMain)
  59. $WindowMain = [Windows.Markup.XamlReader]::Load($readerMain)
  60.  
  61. #region Controls
  62. $btnTest = $WindowMain.FindName("btnTest")
  63. $listBox = $WindowMain.FindName("listBox")
  64. $label = $WindowMain.FindName("label")
  65. #endregion
  66.  
  67. $WindowMain.FindName("btnTest").Add_click({
  68.         $label.Content = "Normal button clicked"
  69.     })
  70.  
  71. $WindowMain.FindName("btnRemoveOSCollEntry").Add_click({
  72.         $label.Content = "DataTemplate button clicked"
  73.     })
  74.  
  75. $DataSource = @()
  76. $obj = New-Object PSCustomObject
  77. $obj | Add-Member -MemberType 'NoteProperty' -Name Title -Value "Test title"
  78. $DataSource += $obj
  79. $listBox.ItemsSource = $DataSource
  80.  
  81. $WindowMain.ShowDialog() | Out-Null
  82. Exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement