Open WPF Form from VSTO Add-on

I have this code in Thisaddin.cs

public void Search(string input) { ServerList listofservers = new ServerList(); listofservers.Visibility; } 

ServerList is a simple WPF form with a list, but how is it to show listofservers ?

I can not find listofserver.show();

enter image description here

+7
source share
3 answers

So, first of all there is no element named WPF Form, there is only User Control for WPF. So, as soon as the WPF UserControl is created in XAML, you will notice that this is the code

 <UserControl x:Class="SQL_openertak2.ServerList" 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" mc:Ignorable="d" d:DesignHeight="454" d:DesignWidth="259" SizeToContent="WidthAndHeight"> <Grid> <ListBox Height="410" HorizontalAlignment="Left" Margin="12,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="242" /> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,427,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </Grid> </UserControl> 

So, I looked at the XAML code. So, you can see that all this is USERCONTROL you should change it to WINDOW , then you can see .Show ()

But note that you also need to change the code in the xaml.cs file

the default reason will be this:

 public partial class ServerList : UserControl 

Change it to

 public partial class ServerList : Window 

good for obvious reasons !! :)

+15
source

You can also place it in a panel, for example:

  • Open Form1 in the Windows Form Designer.
  • In the toolbar, drag the TableLayoutPanel control to the folder
  • In the TableLayoutPanel smart tag management panel, select Delete Last Row.
  • Resize the TableLayoutPanel control to a large width and height.
  • In the toolbar, double-click UserControl1 to create an instance of UserControl1 in the first cell of the TableLayoutPanel control.
  • UserControl1 instance is hosted in a new ElementHost control named elementHost1.
  • In the toolbar, double-click UserControl1 to create another instance in the second cell of the TableLayoutPanel control.
  • In the Document Structure window, select tableLayoutPanel1. See the Document Structure Window for more information.
  • In the Properties window, set the Fill property to 10, 10, 10, 10.
  • Both ElementHost controls change to reflect the new layout.
0
source

Change UserControl with Window as already answered in XAML and C # class.

Keep in mind that in VSTO applications, which are usually based on Windows Forms, it is important not to forget to add System.XAML to links, otherwise you will probably get errors when creating forms layouts.

This could happen in VS2015, as I recently experienced, where the wizard procedure did not work as expected without updating the class references.

0
source

All Articles