Quick Start - WPF KerbalData - KSP Data Unlocked!

Quickstart Guide for WPF This walkthrough will help you quickly get started with a WPF application that can load KSP data through user selection.

Create a new Project

Procedure #2

  1. First step

  2. Second step

Add KerbalData to your Project

Procedure #2

  1. First step

  2. Second step

Prepare for Development

Procedure #2

  1. First step

  2. Second step

View/ViewModel

Create the ViewModel and Update MainView

  1.                           namespace MyApp
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.ComponentModel;
        using System.Windows.Input;
        using System.Windows.Forms;
    
        using KerbalData;
    
        public class MainWindowViewModel : INotifyPropertyChanged
        {
            const string PropNameKerbalData = "KerbalData";
            const string PropNameInstallPath = "InstallPath";
    
            private string installPath;
            private KerbalData kerbalData;
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            public MainWindowViewModel()
            {
                Init();
            }
    
            public ICommand OpenKspInstallFolderCommand { get; private set; }
    
            public KerbalData KerbalData
            {
                get
                {
                    return kerbalData;
                }
                set
                {
                    if (value != kerbalData)
                    {
                        kerbalData = value;
                        OnPropertyChanged(PropNameKerbalData);
                    }
                }
            }
    
            public string InstallPath
            {
                get
                {
                    return installPath;
                }
                set
                {
                    if (value != installPath)
                    {
                        installPath = value;
                        OnPropertyChanged(PropNameInstallPath);
                        UpdateInstallPath();
                    }
    
                }
            }
    
            private void Init()
            {
                OpenKspInstallFolderCommand = new DelegateCommand(
                    () =>
                    {
                        var dlg = new FolderBrowserDialog();
                        System.Windows.Forms.DialogResult result = dlg.ShowDialog();
    
                        if (result.ToString() == "OK")
                        {
                            InstallPath = dlg.SelectedPath;
                        }
                    });
            }
    
            private void UpdateInstallPath()
            {
                KerbalData = null;
                KerbalData = KerbalData.Create(installPath);
            }
    
            private void OnPropertyChanged(string name, object value = null)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    }
  2. XML
                              <
                              Window x:Class="MyApp.Views.MainWindow" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:local="clr-namespace:KerbalEdit" 
            xmlns:vm="clr-namespace:MyApp.ViewModels" 
            Title="MyApp" Height="768" Width="1360"> 
        <Window.DataContext> 
            <vm:MainWindowViewModel /> 
        </Window.DataContext> 
        <Grid> 
            <TextBox Text="{Binding Path=InstallPath}" 
              HorizontalAlignment="Left" Height="28" Margin="10,10,0,0" 
              TextWrapping="Wrap" VerticalAlignment="Top" Width="999" /> 
            <Button Content="Load" HorizontalAlignment="Left" 
              Margin="1062,10,0,0" VerticalAlignment="Top" Width="35"/> 
            <Button Content="Folder" HorizontalAlignment="Left" Margin="1014,10,0,0" 
              VerticalAlignment="Top" Width="43" Command="{Binding Path=OpenKspInstallFolderCommand}" /> 
        </Grid> 
    </Window>