To add a WPF modeless dialog (typically a PaletteSet) to your AutoCAD plugin project, you have to add a "User Control (WPF)" to the project.
XAML:
<UserControl x:Class="SimpleWpfPalette.PaletteUserControl"
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"
xmlns:local="clr-namespace:SimpleWpfPalette"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="WhiteSmoke">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Margin="5">Enter your name:</Label>
<TextBox x:Name="nameBox" Grid.Row="1" Margin="5"/>
<Button x:Name="okButton" Grid.Row="2" Margin="10" HorizontalAlignment="Center" Content="OK" Height="24" Width="80" Click="okButton_Click"/>
</Grid>
</UserControl>
in the code behind, you can call a custom AutoCAD command with sendStringToExecute to avoid ahaving to lock the current document and set the focus to AutoCAD application:
using System.Windows;
using System.Windows.Controls;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace SimpleWpfPalette
{
public partial class PaletteUserControl : UserControl
{
public PaletteUserControl()
{
InitializeComponent();
}
private void okButton_Click(object sender, RoutedEventArgs e)
{
var doc = AcAp.DocumentManager.MdiActiveDocument;
if (doc != null)
doc.SendStringToExecute("HELLO " + nameBox.Text + "\n", false, false, false);
}
}
}
You add to your project a class for your custom palette set which derives from PaletteSet in which you add the Usercontrol using the AddVisual() method.
using System;
using Autodesk.AutoCAD.Windows;
namespace SimpleWpfPalette
{
public class CustomPalette : PaletteSet
{
public CustomPalette()
: base("WPF Palette", "SHOW_PALETTE", new Guid("{1E20F389-33C1-421F-81CB-B3D413E5B05C}"))
{
Style = PaletteSetStyles.ShowAutoHideButton |
PaletteSetStyles.ShowCloseButton |
PaletteSetStyles.ShowPropertiesMenu;
MinimumSize = new System.Drawing.Size(250, 150);
AddVisual("Hello", new PaletteUserControl());
}
}
}
From an AutoCAD command you show the palette (and create it if it not already exists). this class also contains the command called from the button in the palette.
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
namespace SimpleWpfPalette
{
public class Commands
{
static CustomPalette palette;
[CommandMethod("SHOW_PALETTE")]
public static void ShowPalette()
{
if (palette == null)
palette = new CustomPalette();
palette.Visible = true;
}
[CommandMethod("Hello")]
public static void Hello()
{
var ed = Application.DocumentManager.MdiActiveDocument.Editor;
var pr = ed.GetString("\nEnter your name: ");
if (pr.Status == PromptStatus.OK)
Application.ShowAlertDialog("Hello " + pr.StringResult);
}
}
}