By Xiaodong Liang
WPF is widely used nowadays. Navisworks .NET API supports it. I plan to write the posts to dig some useful cases with WPF. I’d also like to hear any of your suggestion or I’d appreciate if you could share your use case.
The first post is to demo how to create a .NET control application of WPF. Actually there are already 3 samples in SDK folder: <Navisworks Installtation Path>\api\net\examples\Basic Examples\CSharp\WPF. They are well written. In this post, I will analyzed the skeleton and introduce the basic steps to create such application.
Firstly, choose the template of WPF Application.
Next, add the two assemblies
Because .NET control of Navisworks is still a Windows Form control. It needs to be hosted in WindowsFormsHost. So we need to add one more assembly WindowsFormsIntegration.
WPF allows you to design the interface flexibly. They are configured by the XAML file. If you open MainWindow.xaml file, you can see some default lines which are added by the VS template. On the basis of the lines, we will need to import the Navisworks assemblies and WindowsFormsIntegration. e.g.
Next, add one button which will open the Navisworks file. And the .NET view control of Navisworks.
Where x:Name is the name we could use behind the interface. The final XAML is as below. The default lines are marked in grey.
Title="MainWindow" Height="350" Width="525"
<Button Content="Open File" Height="23" Name="button1" Width="75" Click="button1_Click" />
The last is to implement the code behind. As always, to work with the .NET control, we need to initialize the application control before the application runs, and terminate it after it is closed. So in the construction method of MainWindow , we initialize the application, in the mean time, create a document control to bind with the view control. In Window_Closed, terminate application control.
As to open file, it is very simple. Please refer to the code.
/// Interaction logic for MainWindow.xaml
public partial class MainWindow : Window
DocumentControl documentControl = new DocumentControl();
// initialize the control application
viewControl.DocumentControl = documentControl;
private void button1_Click(object sender, RoutedEventArgs e)
//Dialog for selecting the Location of the file
OpenFileDialog dlg = new OpenFileDialog();
//Ask user for file location
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
//If the user has selected a valid location,
// then tell DocumentControl to open the file
//As DocumentCtrl is linked to ViewControl
private void Window_Closed(object sender, EventArgs e)
// terminate the control application
Build the program and run it. Our first WPF application was born!
It looks OK, but we seem not find any difference with a Windows Form application. I am far from being an expert of WPF. As I understand, WPF could be looked as the successor of Windows Form, but the WPF
control is extremely flexible. It allows for full visual customization.
In the next post, we will explore how to bind the model hierarchy to the treeview, which has full support for data binding.