1

I have a XAML Windows class

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:ObjectReferenceSample" 
    Title="ObjectReference Sample" Height="300" Width="300">
    <Window.DataContext>
    <ObjectDataProvider ObjectType="viewModel:MyViewModel">
      <ObjectDataProvider.ConstructorParameters>
        **<!-- MyModel class instance defined in codebehind-->****
      </ObjectDataProvider.ConstructorParameters>
    </ObjectDataProvider>
    </Window.DataContext>
</Window>

Also in my code behind I have an instance of MyModel class that needs to be passed as constructor parameter to my ViewModel through ObjectDataProvider.

public partial class MyWindow: Window
{
    public MyModel model;
    public MyWindow()
    {
          model = new MyModel();  
    }
}

MyViewModel would like:

public partial class MyViewModel
{
    public MyViewModel(MyModel modelInstance)
    {

    }
}

I have some limitation of not being able to use DI like Unity. Please suggest me solution of above problem without DI concept only.

ABC DEF
  • 189
  • 2
  • 14
  • 2
    Why not set this DataContext in code behind also? You have your model there already anyway. – Evk Dec 02 '17 at 18:05
  • This is an example code i shared. In my actual code i have few more constraints so i am bound to achieve above flow only – ABC DEF Dec 02 '17 at 18:13
  • @ABCDEF What constraints? Anyhow, you can initialize properties when you create the instance in XAML, but the constructor must be parameterless. – 15ee8f99-57ff-4f92-890c-b56153 Dec 02 '17 at 19:30
  • 1
    I don't understand what you're asking. Does the code you posted work? It seems to me it should, depending on what's actually in the part **you didn't bother to show**. What does the code do? What did you want instead? What _specifically_ do you need help with? – Peter Duniho Dec 02 '17 at 19:46

0 Answers0