Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

WPF bindings broken in Revit plugin

12 REPLIES 12
Reply
Message 1 of 13
emil-poulsen
1254 Views, 12 Replies

WPF bindings broken in Revit plugin

Hi All,

I'm developing a plugin for Revit 2017, let's call it MyAddin.dll.

 

MyAddin.dll is dependent on MyLibrary.dll, which is dependent on a number of external libraries, such as WPF and helix.toolkit (3d visualization library). When running the plugin, the idea is to show a new WPF window with a custom viewport (see code below). This works fine - i.e. the modal is instantiated and the helix toolkit viewport aggregated in the WPF window pops up and looks seemingly good. However, the WPF bindings in the app seem to be completely broken, which means I can't add any items into my helix toolkit scene or update the content of the app at runtime.

 

 

    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class MyAddinClass : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) {

            //Get application and document objects
            UIApplication uiapp = commandData.Application;
            Document doc = uiapp.ActiveUIDocument.Document;
//create the plugin and run it. MyLibrary.MainWindow win = new MyLibrary.MainWindow(); win.ShowDialog(); return Result.Succeeded; } }

 

 

I do not get any exceptions thrown.

 

For the sake of testing, I've tried running the exact same code (see below) from other environments (custom .exe and Rhino 6), which is working fine - i.e. all WPF bindings are still intact.

 

//create the plugin and run it.
MyLibrary.MainWindow win = new MyLibrary.MainWindow();
win.ShowDialog();

 

Does anyone have an idea what I could do to get closer to a solution? Any information would be very much appreciated.

 

Many thanks,

Emil

12 REPLIES 12
Message 2 of 13
FlorisvdG
in reply to: emil-poulsen

Could you maybe post the code where you specify the datacontext?

Message 3 of 13
emil-poulsen
in reply to: FlorisvdG

Hi @FlorisvdG

Thanks for your reply. Sure, this is what I've got in my xaml file:

 

 

 <Window.Resources>
<local:DataTemplate3D x:Key="{x:Type local:ModelElement}">
<ModelVisual3D Content="{Binding Model}">
<ModelVisual3D.Transform>
<TranslateTransform3D OffsetX="{Binding Position.X}" OffsetY="{Binding Position.Y}" OffsetZ="{Binding Position.Z}" />
</ModelVisual3D.Transform>
</ModelVisual3D>
</local:DataTemplate3D>
</Window.Resources>

 

 

and  the actual viewport element:

 

 

 <HelixToolkit:HelixViewport3D ZoomExtentsWhenLoaded="True" Grid.Column="1" Grid.Row="0">
<local:ItemsVisual3D ItemsSource="{Binding ObservableElements}" RefreshChildrenOnChange="{Binding IsChecked, ElementName=refreshOnChange, Mode=OneWay}" />
</HelixToolkit:HelixViewport3D>

 

I am following one of the official Helix examples here: https://github.com/helix-toolkit/helix-toolkit/blob/develop/Source/Examples/WPF/ExampleBrowser/Examp...

 

Thank you

Message 4 of 13
FlorisvdG
in reply to: emil-poulsen

You might want to try setting the dataxontext explicitly.

 

for instance in your mainwindow class:

 

public partial class Mainwindow : Window

{

private Model model =  new Model(); // this would be the instance of the class in which your data is stored('binds to')


public Mainwindow()

{
this.Datacontext = model;

InitializeComponent();
}
}

 

Message 5 of 13
emil-poulsen
in reply to: FlorisvdG

Hi @FlorisvdG

Yep, that's being done in the constructor currently:

 

public MainWindow() {
    //set some properties..
    this.DataContext = this;
}

What bugs me is that I'm able to get the binding to work perfectly when it's launched from other platforms, such as custom .exe:

 

[STAThread]
static void Main(string[] args) {
    MyLibrary.MainWindow win = new MyLibrary.MainWindow();
    win.ShowDialog();            
}

and rhino:

 

protected override Result RunCommand(RhinoDoc doc, RunMode mode) {
    MyLibrary.MainWindow win = new MyLibrary.MainWindow();
    win.ShowDialog();
    return Result.Success;
}

Do you think there could be anything in the way Revit launches plugins that can affect bindings?

 

Thanks again

Message 6 of 13
FlorisvdG
in reply to: emil-poulsen

That's very weird indeed. I've used WPF with Revit and it always works fine. I don't use extra dll's like you do though...

Did you try debugging it, and checking the output..?

 

Message 7 of 13
amesisamess
in reply to: emil-poulsen

Any updates on this? I suspect I'm having a similar problem and was curious to know if/what your solution was.

Message 8 of 13
cwaluga
in reply to: amesisamess

Integration of Helix seems kind of tricky. Jeremy did a blog post on a similar topic recently: https://thebuildingcoder.typepad.com/blog/2019/11/integrating-the-helix-3d-viewer-with-a-wpf-add-in....

Message 9 of 13
emil-poulsen
in reply to: amesisamess

Hi,

No, we never got around to fix the binding issue. Instead we wrote the helix application as a standalone executable (running on its own window/thread), which is launched from a lightweight Revit plugin.

Message 10 of 13
amesisamess
in reply to: emil-poulsen

I had a feeling that was going to be the case. Thanks for the update! 

Maybe you don't recall since this was an old thread I chimed in on, but do you remember if you could update any other WPF bindings, ie a label or textbox? I found I was able to update regular WPF control bindings but if a property was bound to a Helix object, it wouldn't update. 

For anyone else who stumbles upon this thread, you'll probably have to rewrite your plugin as a standalone executable that communicates with Revit via IPC.
https://thebuildingcoder.typepad.com/blog/2019/11/integrating-the-helix-3d-viewer-with-a-wpf-add-in....

https://thebuildingcoder.typepad.com/blog/2019/04/set-floor-level-and-use-ipc-for-disentanglement.ht...

Message 11 of 13
sonicer
in reply to: amesisamess

It's case sensitive.. bind model instead Model.
Message 12 of 13
sonicer
in reply to: amesisamess

It's case sensitive.
Message 13 of 13
sonicer
in reply to: amesisamess

It's case sensitive. Bind model instead Model.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Rail Community