Hydra host app and WPF binding did not work

Hi,

my problem as is described in title is about to make binding work in my wpf app (user control) hosted in delphi. Host app is (from bl point of view) the same as in visualizer example. WPF is divided to two assemblies (UI “as xaml, code behind, embeded resources” and Client “as portable dll + referenced dlls”).
Currently I am running on hydra trial (as proof of concept). My component get loaded from visualize point of view but data binding is not working. Also when I try to run my WPF app in debug mode hosted in delphi app I see that exposed WPF API (in wrapper class) are not getting populated when I type something to my textboxes (WPF app with textboxes hosted in delphi).

In Xaml, I am using

DataContext="{Binding ViewModel, RelativeSource={RelativeSource Self}}"

as a reference to view model (stored in code behind) and then Prism as binding frw.

example syntax in xaml:

Thanks in advance. M.

Hello, could you please povide simple testcase, so I can reproduce this situation here?
Thanks.

Hi Katerina,

this question is most technically related and I am not sure if is even possible to create test case.

If I reference to your demo application (Visualizer: wpf comp + hydra + delphi) example, you are not working with any dynamic binding (ViewModel) but based on exposed properties in wrapper class,you are setting public properties in control - code behind (VisualizerPlugin.xaml.cs) which sets control elements data sources (association is created by control ui element name in code behind).

My question is, how to make it run in way that ViewModel is created dynamically (WPF IoC) in UI thread to use MVVM.
I have a suspicion that IoC is not working and then my ViewModel get not created.

Is here some another approach which could help me to go thru of this?

Thanks in advance.

Hello,
I believe that you didn’t properly set up bindings in your application. I just tried to modify Visualizer sample to have binding to BarsVisible property and it worked just fine.
Consider that if you want to bind directly to code behind properties you should use DataContext="{Binding RelativeSource={RelativeSource Self}}" in xaml. If you have separate class for view model referenced by ViewModel property in the code behind, you should use

public string RulesetName
{
get { return PluginInstance.ViewModel.RulesetName; }
set { PluginInstance.ViewModel.RulesetName= value; }
}

Hi Katerina,

thanks for reply. Could you provide an example here how did you bind the data by

DataContext=“{Binding RelativeSource={RelativeSource Self}}”

because I used this approach before and it didn’t work.

I made it work by adding new event handler for “control get loaded” and I bound the UI elements with data source (VM) there. Data context declaration in xaml refers to VM data type:

DataContext=“{Binding ViewModel, RelativeSource={RelativeSource Self}}” Loaded=“Hydra_Loaded”>

In event handler (Hydra obj is the name of the VisualPlugin in xaml):

Hydra.DataContext = _viewModel;

Why it is not working in the first example is the question.

My guess is because UI thread (when initializes UI elements) looks also for dependencies (based on path binding) and tries to inject data. In this case it is not happening and VM is not found (by reflection).

To look into visualizer example, you are using approach where code behind is responsible for data binding (you find an element and directly set the datasource => which seems to me like a tightly coupled solution). My project was another way around. XAML (UI) was responsible to find an object for binding.

Looking forward for you example, I think I am missing something here.

Thanks in advance.

Hello,
I can try to help you with your project and see why it isn’t working, if you provide me with simple testcase.
Here, in the Visualizer example, I just tried to modify it and bind to codebehind properties (to do this I used DataContext="{Binding RelativeSource={RelativeSource Self}}" in XAML code of plugin, and implemented INotifyPropertyChanged to trigger proprty changing in the codebehind. And it worked just fine. So, I think your problem is not related to Hydra itself, but to tuning of your WPF part of the project.
Btw, by
DataContext="{Binding ViewModel, RelativeSource={RelativeSource Self}}" Loaded=“Hydra_Loaded”> and Hydra.DataContext = _viewModel; you are doing the same thing in different places.

Hi Katerina,
correct me if I am wrong but from what you’ve mentioned: you promote fRectangles private field to public property in code behind and then in xaml, base on Path binding source declaration, you’ve set MainGrid UI element datasource to this property?

I think I don’t follow you with “providing test case”. The problem I am facing with is more technical issue with binding.

Scenario as I mentioned before is:

  1. after hosting delphi app start component is displayed
  2. data binding is not performed automatically in WPF user control component (in load lifetime)

But after when I’ve linked my VM as datasource to UI element in code behind directly it works.

… or maybe better approach - can I ask you for a code behind file example to see the your changes?
It might bring more light to this problem.

Thanks in advance

Hello,
please take a look at modified WPF plugin that have binding to the BarsVisible property in the checkbox.
Visualizer Plugin (C#).zip (14.6 KB)

Thanks Katerina, problem solved!