【发布时间】:2016-12-12 19:43:56
【问题描述】:
我的 WPF 应用程序不使用 app.xaml。我正在使用 MVVM,并分别构建视图和视图模型。然后我将 ViewModel 传递给 View 构造函数并在那里设置数据上下文。
我希望 Visual Studio 能够理解视图模型的属性以进行上下文单击等,但在设置视图时不让它构造自己的 DataContext。
当我这样做时,它会强制我为 MainViewModel 提供一个默认构造函数,然后在我构造视图时调用该 VM 构造函数。
<Window x:Class="Kraken.CopFeed.Windows.MainFeedView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Windows="clr-namespace:MyProgram.MainApp.WpfWindows"
mc:Ignorable="d"
Title="Main App" Height="321.557" Width="652.922">
<Window.DataContext>
<Windows:MainViewModel />
</Window.DataContext>
如何保持现有的分别构建 V 和 VM 的实现,但让 Visual STudio 中的 XAML 编辑器知道我的 ViewModel(最终)将被设置为数据上下文?
【问题讨论】:
-
我认为你可以用
d:DesignInstancestackoverflow.com/questions/8303803/…来做到这一点 -
该死的,designInstance 做到了。你能补充一下答案吗?