Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
The requirement is to display a user control in a tab item. The below is the code I am trying.

PowerShell
Add-Type -AssemblyName PresentationFramework
[xml]$MainXAML = @"
<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="42*" />
            <RowDefinition Height="269*" />
        </Grid.RowDefinitions>
            <Button Name="Button1" Content="ClickMe" Width="61" Height="20" Margin="12,12,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"></Button>
        <TabControl Name="tabChildContainer" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="1"></TabControl>

    </Grid>
</Window>
"@
[xml]$UserControlXAML = @"
<UserControl 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             Height="292" Width="489">
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="150,86,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</UserControl>

"@

$MainReader = New-Object System.Xml.XmlNodeReader $MainXAML
$Window_Main= [Windows.Markup.XamlReader]::Load($MainReader)

$UC_Reader = New-Object System.Xml.XmlNodeReader $UserControlXAML
$Window_UC= [Windows.Markup.XamlReader]::Load($UC_Reader)


$MainXAML.SelectNodes("//*[@Name]") | %{ Set-Variable -Name ("WPF_Main_"+$_.Name) -Value $Window_Main.FindName($_.Name) -Scope "Script" }
$UserControlXAML.SelectNodes("//*[@Name]") | %{ Set-Variable -Name ("WPF_UC_"+$_.Name) -Value $Window_UC.FindName($_.Name) -Scope "Script" }
$script:WPF_Main_button1.Add_Click(
{
    Write-Host -ForegroundColor Green "I am in the click event."
    $TabItem1 = New-Object System.Windows.Controls.TabItem
    $TabItem1.Header = "Eureka"
    $TabItem1.AddChild($WPF_UC_button1)
    $WPF_Main_tabChildContainer.Items.Add($TabItem1)
})
$Window_Main.ShowDialog() 

The below is the error. Could someone pls help me to fix it? thank you in advance.
PowerShell
Exception calling "ShowDialog" with "0" argument(s): "Must disconnect specified child from current parent Visual before attaching to new parent Visual."
At line:50 char:1
+ $Window_Main.ShowDialog()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentException


What I have tried:

hi,
I am trying to use a WPF User control in PowerShell. Everything is working fine but when I select the tab item then getting the focus error.
Posted
Updated 2-Jun-20 17:22pm

1 solution

change
$TabItem1.AddChild($WPF_UC_button1)

to
$TabItem1.AddChild($Window_UC)


and it worked for me
 
Share this answer
 
Comments
Dave Kreskowiak 3-Jun-20 0:49am    
I seriously doubt the OP is still looking for an answer 4 years later.
Member 14851617 22-Jun-20 23:45pm    
That's very fair, but if anyone else encounters this post there is now an answer. lol
Dave Kreskowiak 23-Jun-20 0:25am    
Creating user controls in Powershell is a very narrow market.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900