Object作为XAML中DataContext的构造函数参数

时间:2017-12-02 17:26:31

标签: c# wpf xaml objectdataprovider

我有一个XAML Windows类

<div class="rows">
  <div class="col1">
     <div class="columns">
      <img style="width:100%;" src="https://dummyimage.com/600x400/000/fff" />
      <p>text</p>
    </div>
    <div class="columns">
    <img style="width:100%;" src="https://dummyimage.com/600x400/000/fff" />
      <div class="rows">
      <p>text</p>
      <p>text</p><p>text</p><p>text</p><p>text</p><p>text</p>
      <p>text</p><p>text</p><p>text</p><p>text</p><p>text</p>
      </div>
    </div>
  </div>
  <div class="col2">
   <div class="columns">
      <img style="width:100%" src="https://dummyimage.com/600x400/000/fff" />
      <div class="rows"><p>text</p> <p>text</p> <p>text</p> </div>
      <div class="rows"><p>text</p> <p>text</p> <p>text</p> </div>
      <div class="rows"><p>text</p> <p>text</p> <p>text</p> </div>
    </div>
    <div class="columns">
      <img style="width:100%" src="https://dummyimage.com/600x400/000/fff" />
      <p>text</p>
    </div>
    <div class="columns">
      <img style="width:100%" src="https://dummyimage.com/600x400/000/fff" />
      <p>text</p>
    </div>
  </div>
</div>

同样在我的代码后面,我有一个MyModel类的实例,需要通过ObjectDataProvider作为构造函数参数传递给我的ViewModel。

<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>

MyViewModel希望:

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

我有一些限制,无法像Unity那样使用DI。如果没有DI概念,请建议我解决上述问题。

0 个答案:

没有答案