I tried with an ElementHost in a wpf window but I still have the problem of loss of focus
FormElementHost : (lose focus)
- Command :
[Transaction(TransactionMode.Manual)]
public class FormElementHostCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
FormElementHost form = new FormElementHost(commandData);
form.ShowDialog();
return Result.Succeeded;
}
}
- Form (System.Windows.Forms.Form) :
public partial class FormElementHost : Forms.Form
{
ExternalCommandData _commandData;
public FormElementHost(ExternalCommandData commandData)
: this()
{
_commandData = commandData;
}
public FormElementHost()
{
InitializeComponent();
this.userControl11.ClickEvent += UserControl11_ClickEvent;
}
private void UserControl11_ClickEvent(object sender, EventArgs e)
{
Family family;
string message;
UtilsFamily.LoadFamily(_commandData.Application.ActiveUIDocument.Document, out family, out message);
}
}
- UserControl WPF (System.Windows.Controls.UserControl) :
public partial class UserControl1 : UserControl
{
public event EventHandler ClickEvent;
private void RaisesEventClick()
{
if (this.ClickEvent != null)
this.ClickEvent(this, new EventArgs());
}
public UserControl1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
RaisesEventClick();
}
}
FormNoElementHost : (not lose focus)
- Command :
[Transaction(TransactionMode.Manual)]
public class FormNoElementHostCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
FormNoElementHost form = new FormNoElementHost(commandData);
form.ShowDialog();
return Result.Succeeded;
}
}
- Form (System.Windows.Forms.Form) :
public partial class FormNoElementHost : Forms.Form
{
ExternalCommandData _commandData;
public FormNoElementHost(ExternalCommandData commandData)
: this()
{
_commandData = commandData;
}
public FormNoElementHost()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Family family;
string message;
UtilsFamily.LoadFamily(_commandData.Application.ActiveUIDocument.Document, out family, out message);
}
}
WindowWPF: (lose focus)
- Command :
[Transaction(TransactionMode.Manual)]
public class WindowWPFCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
WindowWPF window = new WindowWPF(commandData);
window.ShowDialog();
return Result.Succeeded;
}
}
- Window (System.Windows.Window) :
public partial class WindowWPF : Window
{
ExternalCommandData _commandData;
public WindowWPF(ExternalCommandData commandData)
: this()
{
_commandData = commandData;
}
public WindowWPF()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Family family;
string message;
UtilsFamily.LoadFamily(_commandData.Application.ActiveUIDocument.Document, out family, out message);
}
}
UtilsFamily :
public class UtilsFamily
{
public static bool LoadFamily(Document doc, out Family family, out string message)
{
family = null;
message = null;
// ask user to select a family file
System.Windows.Forms.OpenFileDialog fileDlg = new System.Windows.Forms.OpenFileDialog();
fileDlg.Filter = "Revit family (*.rfa)|*.rfa";
fileDlg.Multiselect = false;
if (fileDlg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
return false;
using (Transaction tr = new Transaction(doc))
{
tr.Start("Load family");
if (!doc.LoadFamily(fileDlg.FileName, new FamilyOption(), out family))
{
message = "Could not load family !";
tr.RollBack();
return false;
}
tr.Commit();
}
return true;
}
}