c# - WPF ContentControl 无法从 ViewModel 解析正确的 View (Catel)

标签 c# wpf mvvm catel

我有一个 WPF 项目,其中 RibbonWindow 作为主窗口,并且我正在使用 Catel。

/ View /主窗口:

public partial class MainWindow : MainWindowBase
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

该MainWindow 派生自MainWindowBase。 MainWindowBase 派生自 RibbonWindow,并实现 IDataWindow,如 Catel 文档中所述。

public class MainWindowBase : RibbonWindow, IDataWindow
{
    private readonly WindowLogic _logic;
    //...
}

MainWindow 由 StartupUri="/Views/MainWindow.xaml"实例化,并解析并实例化正确的 MainWindowViewModel。

在 MainWindow 中,我有一个内容控件,它绑定(bind)到 MainWindowViewModel 中的 Property CurrentViewModel

<ContentControl DockPanel.Dock="Bottom" Content="{Binding Path=CurrentViewModel}"></ContentControl>

/ViewModels/MainWindowViewModel:

public class MainWindowViewModel : ViewModelBase
{
//..
    public static readonly PropertyData CurrentViewModelProperty = RegisterProperty("CurrentViewModel", typeof(ViewModelBase), null);

    public ViewModelBase CurrentViewModel
    {
        get { return GetValue<ViewModelBase>(CurrentViewModelProperty); }
        set { SetValue(CurrentViewModelProperty, value); }
    }
//..
}

此属性设置为同样派生自 ViewModelBase 的 AddressViewModel

/ViewModels/AddressViewModel:

public class AddressViewModel : ViewModelBase
{
}

我有一个从 Catel.Windows.Controls.UserControl 派生的 AddressView: / View /AddressView:

public partial class AddressView : Catel.Windows.Controls.UserControl
{
    public AddressView()
    {
        InitializeComponent();
    }
}

问题在于关联的 AddressView 未解析,内容控件仅显示 AddressViewModel 的名称。

我还向 App.xaml 添加了一个 DataTemplate,如下所示,但没有效果。

<DataTemplate DataType="x:Type vm:AddressViewModel">
    <view:AddressView />
</DataTemplate>

我错过了什么?

谢谢!

最佳答案

看来您基本上忘记了表达式中的括号。应该看起来像这样:

<DataTemplate DataType="{x:Type vm:AddressViewModel}">

关于c# - WPF ContentControl 无法从 ViewModel 解析正确的 View (Catel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20423264/

相关文章:

JavaScriptSerializer Serialize() 方法无法返回非常大的 json 字符串

c# - 如何忽略生成的 JSON 中的空对象文字?

c# - 相当于WPF中的InvokeRequired

wpf - 在 Caliburn 框架中组织 View 和 View 模型

jquery-mobile - jQuery Mobile 和 MVVM 挑战

c# - petapoco 可以处理连接和查询生成的列吗?

c# - TransactionScope 不会在使用 NUnit 和 TestServer 进行集成测试的每次测试 TearDown 中回滚

.net - 将 XAML 控件从 WPF 应用程序复制到类库后出现问题

c# - WPF Mvvm 单选按钮绑定(bind)未从 View 模型设置初始值

c# - 使用带有 MVVM 模式的 WPF 实现进度条(不使用 BackgroundWorker)