0

我想在 WPF MVVM 项目中使用不同的构造函数。第一个以 MDI 形式使用。但是第二个构造函数的使用超过了另一种向第二个发送参数的形式。我该如何使用它?

public MyModelView()
{
    InitializeComponent();
    tlb.SelectVisibility = System.Windows.Visibility.Visible;
}

public MyModelView(MyViewModel test)
{
    InitializeComponent();
    DataContext = test;
    tlb.SelectVisibility = test.Visibility;
}

我有两个构造函数。但我在 xaml 端有一个数据上下文。错误是

Xaml parse exception: 'The invocation of the constructor on type 'XYZ.AP.UI.ViewModel.MyViewModel' that matches the specified binding constraints threw an exception.' Line number '15' and line position '6'.

<Windows:TransactionWindow.DataContext>
    <ViewModel:MyViewModel />
</Windows:TransactionWindow.DataContext>
4

1 回答 1

2

不能使用 XAML 中的参数化构造函数。DataContext您可以通过在创建窗口时在代码中设置窗口来绕过此限制。

但看起来您已尝试将 设置DataContextMyViewModelnot MyModelView。这可能是您异常的来源。

顺便说一句,“ModelView”和“ViewModel”的名称是什么?这些看起来令人困惑的相似......这不是字母 MVVM 所代表的。

于 2012-11-06T13:07:06.337 回答