我想防止在设计时创建ViewModel。
因此,标题看起来像这样:
<UserControl x:Class="app.reports.sta"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:viewmod.reports.sta;assembly=viewmodAss"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
mc:Ignorable="d"
d:DesignHeight="1000" d:DesignWidth="1200"
d:DataContext="{d:DesignInstance Type=vm:staViewModel, IsDesignTimeCreatable=False}"
Language="de-DE">
DataContext的设置如下所示:
<UserControl.DataContext>
<vm:staViewModel/>
</UserControl.DataContext>
<!--With this part active, the designer throws an error from the viewmodel constructor -->
<!--Without this part, the designer works, but when the app is run, not data is displayed (which is quite obivous) -->
如何设置DataContext
,以便在设计时不会创建ViewModel?
MainViewModel,并且控件通过DataBinding获取DataContext。在App.xaml.cs(重写OnStartup)中,将MainWindow的DataContext设置为MainViewModel,然后显示您的MainWindow。请记住清除App.xaml中的StartupUri。