3
WPF MVVM为什么使用ContentControl + DataTemplate视图而不是直接的XAML窗口视图?
为什么这个? MainWindow.xaml: <Window x:Class="MVVMProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid> <ContentControl Content="{Binding}"/> </Grid> </Window> 将您的ExampleView.xaml设置为: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vms="clr-namespace:MVVMProject.ViewModels"> <DataTemplate DataType="{x:Type vms:ExampleVM}" > <Grid> <ActualContent/> </Grid> </DataTemplate> </ResourceDictionary> 并创建如下窗口: public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); MainWindow app = new MainWindow(); ExampleVM context = new ExampleVM(); …
83
c#
wpf
xaml
mvvm
architecture