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:
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.