1

I am trying to pass existing object instances as parameters to the constructor of an object being created using the ObjectDatatProvider. This always fails with the given exception, even though my object has a constructor that takes one parameter:

System.Windows.Data Error: 34 : ObjectDataProvider cannot create object; Type='VegaViewModel'; Error='Wrong parameters for constructor.' MissingMethodException:'System.MissingMethodException: Constructor on type 'WpfApplication1.VegaViewModel' not found. at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) at System.Windows.Data.ObjectDataProvider.CreateObjectInstance(Exception& e)'

Here is my Xaml:

<ObjectDataProvider ObjectType="{x:Type WpfApplication1:VegaModel}" MethodName="Sample" x:Key="VegaPnlData"/>     
<ObjectDataProvider ObjectType="{x:Type WpfApplication1:VegaViewModel}" x:Key="VegaViewModel">
                <ObjectDataProvider.ConstructorParameters>
                    <ObjectDataProvider ObjectInstance="{StaticResource VegaPnlData}"/>
               </ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>

If I use this xaml I get the same error as well:

 <ObjectDataProvider ObjectType="{x:Type WpfApplication1:VegaViewModel}" x:Key="VegaViewModel">
            <ObjectDataProvider.ConstructorParameters>
                <StaticResource ResourceKey="VegaPnlData"/>
            </ObjectDataProvider.ConstructorParameters>
        </ObjectDataProvider>

Here is the code for the VegaViewModel class:

public class VegaViewModel
{            
    public VegaViewModel(VegaModel vegaPnl)
    {
        VegaPnl = vegaPnl;           
    }

    public VegaModel VegaPnl { get; set; }            
}
Kayode Leonard
  • 425
  • 5
  • 12

1 Answers1

0

You can't pass an ObjectDataProvider via the ConstructorParameters. The reason as Dr. WPF adequately states...

An ObjectDataProvider (ODP) is designed to be used with a Binding. If you use it outside of the context of a binding, you end up refering to an instance of the ODP rather than the dereferenced object that it "provides".

Your trying to do something the framework won't allow by design.

Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • So how do I bind to the result of the method and use that as the parameter to the constructor? – Kayode Leonard Jan 20 '11 at 17:55
  • If I do the following I still get the same exception: – Kayode Leonard Jan 20 '11 at 18:04
  • @user583337 You can't do it in your current implementation. Check out the link I posted as Dr. WPF provides alternatives which should suit you. Pushing all this into the XAML is in and of itself a bit strange to be honest...but assuming you have a valid reason to do it. – Aaron McIver Jan 20 '11 at 18:08
  • How would one go about passing the window/user control object as a parameter to the constructor parameters. I'd like my window's data object to have a reference to the window object. – Mike G Oct 12 '11 at 13:11