# WPF UserControl

**URL:** <https://forums.powershell.org/t/wpf-usercontrol/6947>\
**Category:** PowerShell Help\
**Created:** [August 8, 2016, 10:45am UTC](https://forums.powershell.org/t/wpf-usercontrol/6947 "2016-08-08T10:45:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sagar-ps](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@sagar-ps](https://forums.powershell.org/u/sagar-ps)\
**Post date:** [August 8, 2016, 10:45am UTC](https://forums.powershell.org/t/wpf-usercontrol/6947/1 "2016-08-08T10:45:11Z")

</div>

hi,  
The requirement is to display a user control in a tab item. The below is the code I am trying. 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.

I tried to resolve it by own and even tried to seek help from others but unfortunately unable to fix it.

```
Add-Type -AssemblyName PresentationFramework
[xml]$MainXAML = @"

    
        
            
            
        
            
        
 
    

"@
[xml]$UserControlXAML = @"

    
        
    

 
"@
 
$MainReader = New-Object System.Xml.XmlNodeReader $MainXAML
$Window_Main= [Windows.Markup.XamlReader]::Load($MainReader) in 
 
$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()
```

Error that I am getting

```
Hide Copy Code
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
```

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [August 8, 2016, 3:09pm UTC](https://forums.powershell.org/t/wpf-usercontrol/6947/2 "2016-08-08T15:09:44Z")

</div>

Sadly, and I hate to do this, but you might get better results in StackOverflow - more developers there, more playing with WPF. But please, if you do figure it out, a link to the solution will be appreciated by future generations ;).

---

<div class="post-metadata">

**Author:** ![proxb](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/proxb/32/611_2.png) [@proxb](https://forums.powershell.org/u/proxb)\
**Post date:** [August 8, 2016, 7:55pm UTC](https://forums.powershell.org/t/wpf-usercontrol/6947/3 "2016-08-08T19:55:46Z")

</div>

Can you try to re-paste your code? I’m not seeing any of the XAML code so I cannot test to see what the issue might be.

---

<div class="post-metadata">

**Author:** ![sagar-ps](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@sagar-ps](https://forums.powershell.org/u/sagar-ps)\
**Post date:** [August 8, 2016, 9:51pm UTC](https://forums.powershell.org/t/wpf-usercontrol/6947/4 "2016-08-08T21:51:04Z")

</div>

Hi Prox,  
every time the xml part is vanishing. Let me try gist. In other hand may I have your email address so that I can post it to you?

---

<div class="post-metadata">

**Author:** ![sagar-ps](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@sagar-ps](https://forums.powershell.org/u/sagar-ps)\
**Post date:** [August 9, 2016, 9:32am UTC](https://forums.powershell.org/t/wpf-usercontrol/6947/5 "2016-08-09T09:32:36Z")

</div>

Hi,  
I have fixed this issue. I am unable to paste the solved code here since xml part is auto wiping. The solution is, we should create all the controls with in a grid control and add only the grid control to the target control 🙂

---

<div class="post-metadata">

**Author:** ![powercoder](https://avatars.discourse-cdn.com/v4/letter/p/8e8cbc/32.png) [@powercoder](https://forums.powershell.org/u/powercoder)\
**Post date:** [May 27, 2018, 7:29pm UTC](https://forums.powershell.org/t/wpf-usercontrol/6947/6 "2018-05-27T19:29:28Z")

</div>

Hi Sagar,

Would you please provide an example in plain text? I am running into the same problem using User Control within PowerShell.

As you can see from my example below, I get a namespace error when I try implementing UserControls within PowerShell. Am I wrong, is it possible? Any help is appreciated.

For example:  
If I tried implementing a XAML User Control and binding it with PowerShell but it fails with a namespace error. My test project has two XAML files and one PS1 script that can be found in my GitHub.

GitHub  
[GitHub - dlocoder/XAMLwPS: XAML with PowerShell](https://github.com/dlocoder/XAMLwPS).

Project files

1. MainWindow.xaml
2. MyListBoxItem.xaml
3. UsrControl.PS1

PowerShell error:  
PS C:\WINDOWS\system32\> C:\Temp\UserControl\usrcontrol.ps1  
Get-ChildItem : Exception calling “Load” with “1” argument(s): “Cannot create unknown type ‘{clr-namespace:Test.Controls;assembly=Test.Controls}MyListBoxItem’.”  
At C:\Temp\UserControl\usrcontrol.ps1:68 char:8

- $wpf = Get-ChildItem -Path $path -Filter \*.xaml -file | Where-Object …
- 

```
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : NotSpecified: (🙂 [Get-ChildItem], MethodInvocationException
  - FullyQualifiedErrorId : XamlParseException,Microsoft.PowerShell.Commands.GetChildItemCommand

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:35pm UTC](https://forums.powershell.org/t/wpf-usercontrol/6947/7 "2024-05-16T20:35:01Z")

</div>

Closed on May 16, 2024, 8:35pm UTC


