WPF-MVVM Light,如何绑定/设置ContentControl的内容
我是WPF的新手,我想创建一个基于MVVM的应用程序,它使用一个主窗口作为shell,有一些菜单项,当我单击这些菜单项时,WPF-MVVM Light,如何绑定/设置ContentControl的内容,wpf,mvvm-light,Wpf,Mvvm Light,我是WPF的新手,我想创建一个基于MVVM的应用程序,它使用一个主窗口作为shell,有一些菜单项,当我单击这些菜单项时,ContentControl的Content属性就会改变 我通过Nuget添加了MVVM Light,现在我有了一个带有ViewModelLocator和MainViewModel的ViewModel文件夹 My MainWindow.xaml如下所示: <Window x:Class="WpfMvvmApp.MainWindow" xmlns="http://
ContentControl
的Content属性就会改变
我通过Nuget添加了MVVM Light,现在我有了一个带有ViewModelLocator
和MainViewModel
的ViewModel文件夹
My MainWindow.xaml如下所示:
<Window
x:Class="WpfMvvmApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="470" Width="900" IsTabStop="False" AllowsTransparency="True" Background="{x:Null}" BorderBrush="#FF3F3F3F" PreviewMouseMove="HandlePreviewMouseMove"
SnapsToDevicePixels="True" TextOptions.TextFormattingMode="Display" TextOptions.TextRenderingMode="ClearType" ResizeMode="NoResize" WindowStyle="None" WindowStartupLocation="CenterOwner"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid>
<Border x:Name="m_edgeBorder" x:FieldModifier="private" Margin="10" Background="White" IsHitTestVisible="False" IsEnabled="False">
<Border.Effect>
<DropShadowEffect Opacity="0.999" BlurRadius="16" ShadowDepth="0" />
</Border.Effect>
</Border>
<Grid x:Name="m_contentGrid" x:FieldModifier="private" Background="White" Margin="13">
<Rectangle Height="28" VerticalAlignment="Top" Fill="White" PreviewMouseDown="HandleHeaderPreviewMouseDown" />
<Button HorizontalAlignment="Right" Margin="500,6,8,0" VerticalAlignment="Top" Style="{StaticResource ChromeButtonStyle}" Click="HandleCloseClick">
<TextBlock TextWrapping="Wrap" Text="r" FontFamily="Webdings" Foreground="#FF919191" FontSize="13.333" />
</Button>
</Grid>
<ContentControl x:Name="ActiveItem" Content="{Binding CurrentViewModel , Mode=OneWay}" Background="Transparent" HorizontalAlignment="Stretch" IsTabStop="False" Focusable="False" HorizontalContentAlignment="Center"
VerticalContentAlignment="Center" VerticalAlignment="Bottom" Margin="13,0,12,13" MaxHeight="375" />
</Grid>
</Window>
假设我有一个NetworkView.xaml文件,它的视图模型称为NetworkViewModel
<DataTemplate DataType="{x:Type local:NetworkViewModel}">
<local:NetworkView />
</DataTemplate>
MVVM Light用于绑定ContentControl
的Content
属性的预期用途是什么?我假设您必须设置MainViewModel
类的CurrentViewModel
属性,但我不知道如何连接到视图,也不知道如何控制它
有人能举个例子吗?你说得对,我看到的唯一缺失的是一个数据模板,告诉应用程序如何显示NetworkViewModel
<DataTemplate DataType="{x:Type local:NetworkViewModel}">
<local:NetworkView />
</DataTemplate>
这将告诉WPF,无论何时尝试显示网络视图模型
,它都应该使用网络视图
控件谢谢。正是我需要的。