0

I'm trying to create a RDP manager for myself. Since a RDP manager adds and closes tabs dynamically during the use I need this to work properly in WPF.

If I add a function (method) to the button, inside the tabitem template, it works perfectly fine in visual studio. When i copy over the XAML to my PowerShell script, I cannot run a function when that button is being pressed. Since the button is inside a template i have no way to access the button controls inside the PowerShell script to create an add_click({ #somecode }).

This is my WPF tabcontrol:

 <TabControl Name="MainTabControlRDPPages" ItemsSource="{Binding Tabs}">
  <TabControl.ItemContainerStyle>
    <Style TargetType="TabItem">
      <Setter Property="HeaderTemplate">
        <Setter.Value>
          <DataTemplate DataType="local:TabViewModel">
            <Grid>
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
              </Grid.ColumnDefinitions>
              <TextBlock Grid.Column="0" Text="{Binding}"/>
              <Button Grid.Column="1" x:Name="button_close" Click="RunPowershellTest">
                <Button.Template>
                  <ControlTemplate TargetType="Button">
                    <Path Data="M0,0 L8,8 M8,0 L0,8" StrokeThickness="3" VerticalAlignment="Center" Margin="5,4,0,2">
                      <Path.Style>
                        <Style TargetType="{x:Type Path}">
                          <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="False">
                            <Setter Property="Stroke" Value="LightGray" />
                          </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Stroke" Value="Black" />
                          </Trigger>
                          </Style.Triggers>
                        </Style>
                      </Path.Style>
                    </Path>
                  </ControlTemplate>
                </Button.Template>
              </Button>
             </Grid>
            </DataTemplate>
          </Setter.Value>
        </Setter>
      </Style>
  </TabControl.ItemContainerStyle>

  <TabItem Header="No Session" />
</TabControl>

I'm using this code in PowerShell to read all the controls and give them a name.

$reader =(New-Object System.Xml.XmlNodeReader $xaml)
$Window = [Windows.Markup.XamlReader]::Load( $reader )
$xaml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name 
($_.Name) -Value $window.FindName($_.Name) -Scope Script }

How do I make sure I can create a click event for the button_close button? If there's an option to manually add a WPF formatted tabitem during Powershell execution without specifying a template I'm fine with that too.

UPDATE

The button only needs to close the tabpage. If this can be done within XAML i'm fine with that.

jr13216
  • 3
  • 4
  • You have to find the control (`$x = $Window.FindName('name')`, then assign the event handler to it (`$x.add_Close({})`). – Maximilian Burszley Apr 06 '18 at 16:52
  • The control is in a template, powershell returns $null for the button name. I'm unable to find the control this way. – jr13216 Apr 06 '18 at 17:00
  • I don't use wpf with powershell I just do wpf. So this might not be practical. You've presumably got all that XAML in some template somewhere. Could you not associate click with a fixed event handler within that? Is #SomeCode actually going to be dynamic or is it literally just to get a reference to the tabcontrol and remove it's parent tab item out that tabcontrol? – Andy Apr 06 '18 at 18:29
  • I only need the button to remove It's parent tab. An implementation within XAML is fine for me if posible. – jr13216 Apr 06 '18 at 18:57

1 Answers1

0

Thanks to the help of someone inside a PowerShell facebook group I have the solution below. This creates a handler for the tabcontrol where it checks if a button was pressed inside the header. If yes, proceed with closing the tabpage.

[System.Windows.RoutedEventHandler]$EventontabControl = {
    $script:headerName = $_.OriginalSource
    Write-Host $headername

    $script:MainTabControlRDPPageToClose = $_.Source
    $MainTabControlRDPPages.Items.Remove($MainTabControlRDPPageToClose)
}
$MainTabControlRDPPages.AddHandler([System.Windows.Controls.Button]::ClickEvent, $EventontabControl)
jr13216
  • 3
  • 4