Code example: WPF beam application
Updated:
6 Aug 2020
This WPF code example shows in detail how to create a simple WPF application. Application uses the Model API to create a beam in a Tekla Structures model. You will insert beam between two points by choosing the points in Tekla Structures.
After downloading the TSOpenAPIExamples package from GitHub (button above), find WpfBeamApplication example at \Model\Applications\WpfBeamApplication\.
Explanation of the WPF code example
- The MainWindow dialog is inherited from WPF base class ApplicationWindowBase
- The view model class MainWindowViewModel contains dialog attributes. Dialog data is bind to Tekla Structures data attributes with StructuresDialog custom attribute.
#region Properties [StructuresDialog("name",typeof(TD.String))] public string Name { get { return partname; } set { partname = value; OnPropertyChanged("Name"); } }
- PropertyChangedEventHandler of the MainWindowViewModel refreshes dialog values when a view model property changes
public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } }
- The view model class MainWindowViewModel is in the constructor. System will create datastorage based on it.
/// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : ApplicationWindowBase { MainWindowViewModel viewModel = new MainWindowViewModel(); public MainWindow() { InitializeComponent(); this.DataContext = viewModel; this.InitializeDataStorage(viewModel); if(this.GetConnectionStatus()) { InitializeDistanceUnitDecimals(); } }
- The XAML definition of the main window utilizes Tekla Open API's WPF user controls
-
Events are added for button clicks. UI controls, like WpfMaterialCatalog and WpfProfileCatalog, are connected to view model data in event handlers.
<UIControls:WpfSaveLoad HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Top"/> <UIControls:WpfOkCreateCancel HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Bottom" CancelClicked="WPFOkCreateCancel_CancelClicked" CreateClicked="WPFOkCreateCancel_CreateClicked" OkClicked="WPFOkCreateCancel_OkClicked"/> <UIControls:WpfMaterialCatalog x:Name="materialCatalog" HorizontalAlignment="Left" Margin="312,185,0,0" VerticalAlignment="Top" SelectClicked="materialCatalog_SelectClicked" SelectionDone="materialCatalog_SelectionDone_1"/> <UIControls:WpfProfileCatalog x:Name="profileCatalog" HorizontalAlignment="Left" Margin="312,111,0,0" VerticalAlignment="Top" SelectClicked="profileCatalog_SelectClicked" SelectionDone="profileCatalog_SelectionDone"/>
private void profileCatalog_SelectClicked(object sender, EventArgs e) { this.profileCatalog.SelectedProfile = this.viewModel.Profilename; }