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.