尝试这样的事情,一个带有内容控件的 wpf 窗口,该控件绑定到视图模型上的 usercontrol 属性:
<Window
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"
mc:Ignorable="d"
x:Class="MainWindow"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
DataContext="{Binding Main_VM, Source={StaticResource Locator}}"
Background="#FF1D1D1D"
WindowState="Maximized"
WindowStyle="None"
WindowStartupLocation="CenterScreen" ResizeMode="CanResizeWithGrip"
MinHeight="750" MinWidth="1050">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinHeight="700" MinWidth="1000">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<ContentControl Name="UC_Main" Content="{Binding UC_Main}" Grid.Column="1" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<!--workspace user control goes here-->
</ContentControl>
</Grid>
</Window>
您可以使用一些按钮或列表视图等来更改 usercontrol 属性的值。以下是hte view的viewmodel:
Public Class MainWindowViewModel
Inherits ViewModelBase
#Region "DECLARATIONS"
Public Const CC_Main As String = "UC_Main"
Private _ucMain As UserControl = Nothing
#End Region
#Region "PROPERTIES"
Public Property UC_Main() As UserControl
Get
Return _ucMain
End Get
Set(value As UserControl)
If _ucMain Is value Then
Return
End If
RaisePropertyChanging(CC_Main)
_ucMain = value
RaisePropertyChanged(CC_Main)
End Set
End Property
#End Region
#Region "COMMANDS"
#End Region
#Region "CONSTRUCTOR"
Public Sub New()
UC_Main = New YourUserControl
End Sub
#End Region
#Region "METHODS"
#End Region
End Class
显然,这些都已被简化,但应该向您展示什么是可能的。YourUserCONtrol 是您希望在主窗口的内容控件中显示的视图。然后,您可以在按钮或事件上使用 mvvm-light relay 命令将用户控件更改/设置为新控件。您可以根据需要在页面上拥有任意数量的内容控件。