views:

628

answers:

3

How do you pass "this" to the constructor for ObjectDataProvider in XAML.

Lets say my presenter class is:

public class ApplicationPresenter(IView view){}

and that my UserControl implements IView.

What do I pass to the ConstructorParameters in the code below so that the UserControl can create the ApplicationPresenter using the default constructor?

<ObjectDataProvider x:Key="ApplicationPresenterDS" 
ObjectType="{x:Type Fenix_Presenters:ApplicationPresenter}" 
ConstructorParameters="{ ?? what goes here ??}" d:IsDataSource="True" />

I only need to do this so that I can use Blend 2. I know that I can do this in the code behind, but if I do I can't instantiate the class from within Blend. I also know that I can create a parameterless constructor for ApplicationPresenter and pass it a dummy class that implements IView, but I would rather do this in markup if at all possible.

My code behind at the moment is:

public MyUserControl()
{
    InitializeComponent();
    DataContext = new ApplicationPresenter(this); 
}
A: 

i don't know if it works, but you could give your user control a name , e.g.

x:Name="myUserCotrol"

and then use it in a binding:

... ConstructorParameters="{Binding ElementName=myUserControl}" ...

this could work

Joachim Kerschbaumer
This won't work because the ObjectDataProvider does not derive from DependencyObject, so therefore ConstructorParameters is not a DependencyProperty, which is a requirement for DataBinding.
Ian Oakes
+1  A: 

I'm just starting with Wpf and was under the misapprehension that I should be trying to do everything in XAML. I've just watched a few videos from WindowsClient.net which are starting to clear some things up. But boy is this a complex technology!!!

Jeremy Holt
A: 

This will be directly supported (if memory serves well) in the next version of XAML as demonstrated by Rob Relyea at this year's PDC.

Philipp Schmid