Cannot add content to object of type 'Microsoft.Toolkit.Wpf.UI.XamlHost.WindowsXamlHost'

Andrew Azz 171 Reputation points
2021-02-11T17:48:48.13+00:00

Hi Q&A,

The error MC3028 'Cannot add content to object of type 'Microsoft.Toolkit.Wpf.UI.XamlHost.WindowsXamlHost'' appears when trying to add any control (in the conventional manner) to the StackPanel.

I am new to xaml islands; however, there must be some way to add controls, eg text boxes, otherwise what good is it?

What is singularly frustrating, is that all the documentation/samples I can find, either from microsoft or bloggers, only shows a WindowsXamlHost button added to a wpf page.

        <xamlhost:WindowsXamlHost x:Name="StackPanel" InitialTypeName="Windows.UI.Xaml.Controls.StackPanel"  >
*-> any control added here gives the error*
      </xamlhost:WindowsXamlHost>

Any pointers in the right direction would be much appreciated - I've been looking for hours.

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
773 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2021-02-12T04:35:00.893+00:00

    Please refer to WindowsXamlHost control for Windows Forms and WPF to crate a demo to use windowsxamlhost. After
    After successfully adding an demo of Button, please replace the Button by StackPanel as below:

            <xamlhost:WindowsXamlHost x:Name="myStackPanel" InitialTypeName="Windows.UI.Xaml.Controls.StackPanel" Width="600" Height="300" Loaded="myStackPanel_Loaded" />  
      
    

    Its cs code is:

    private void myStackPanel_Loaded(object sender, RoutedEventArgs e)  
            {  
                WindowsXamlHost windowsXamlHost = (WindowsXamlHost)sender;  
      
                Windows.UI.Xaml.Controls.StackPanel myStackPanel =  
                    (Windows.UI.Xaml.Controls.StackPanel)windowsXamlHost.Child;  
      
                myStackPanel.Width = 400;  
                myStackPanel.Height = 200;  
      
                Windows.UI.Xaml.Controls.Button myButton = new Windows.UI.Xaml.Controls.Button() { Content = "Button1", Height = 40, Width = 100 };  
                Windows.UI.Xaml.Controls.TextBlock myTextBlock = new Windows.UI.Xaml.Controls.TextBlock() { Text = "This is TextBlock", Height = 40, Width = 100 };  
                myButton.Click += MyButton_Click;  
      
                myStackPanel.Children.Add(myButton);  
                myStackPanel.Children.Add(myTextBlock);  
            }  
            private void MyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)  
            {  
                MessageBox.Show("This is Windows.UI.Xaml.Controls.Button");  
            }  
    

    The result picture is:
    67242-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Andrew Azz 171 Reputation points
    2021-02-12T10:47:23.803+00:00

    Thank you for the reply; however, are you saying there is no way to do this in xaml?
    I am trying to follow the mvvm pattern, and am using the Microsoft.Toolkit.Mvvm, also from the Community toolkit. As I understand, code behind in the View is not part of the pattern. And since I wanted to use the stack panel in an ItemTemplate with multiple text boxes etc, the code behind solution is not really an option.

    There seems to be a xaml child element tag, which the editor accepts. This gives two errors:
    XDG0038 The property "Child" cannot be empty.
    MC3063 Property 'Child' does not have a value.

     <xamlhost:WindowsXamlHost x:Name="StackPanel" InitialTypeName="Windows.UI.Xaml.Controls.StackPanel"  >
     <xamlhost:WindowsXamlHost.Child>
    
    
     </xamlhost:WindowsXamlHost.Child>
     </xamlhost:WindowsXamlHost>
    

    Also tried:

            <xamlhost:WindowsXamlHost x:Name="StackPanel" InitialTypeName="Windows.UI.Xaml.Controls.StackPanel"  >
                <xamlhost:WindowsXamlHost.Child  x:Name="txtBox" InitialTypeName="Windows.UI.Xaml.Controls.TextBox">
    
                </xamlhost:WindowsXamlHost.Child>
            </xamlhost:WindowsXamlHost>
    

    And also got two errors:
    MC3057 Cannot set properties on property elements.
    XDG0038 The property "Child" cannot be empty.

    If this is not possible, then returning to the old wpf stack panel is not such a problem.