WPF UserControl

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Sagar PS Sagar PS 1 month, 2 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #49571
    Profile photo of Sagar PS
    Sagar PS
    Participant

    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
    
    #49626
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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 ;).

    #49664
    Profile photo of Boe Prox
    Boe Prox
    Participant

    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.

    #49668
    Profile photo of Sagar PS
    Sagar PS
    Participant

    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?

    • This reply was modified 1 month, 2 weeks ago by Profile photo of Sagar PS Sagar PS.
    #49741
    Profile photo of Sagar PS
    Sagar PS
    Participant

    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 🙂

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.