嘗試這樣的事情,用結合的控件屬性上的視圖模型內容控制WPF窗口:
<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>
可以一些按鈕,或ListView等改變的usercontrol屬性的值。以下是hte視圖的視圖模型:
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命令來將usercontrol更改/設置爲新的。您可以根據需要在頁面上添加儘可能多的內容控件。
你的觀點是什麼,他們是頁面,用戶控件等?我使用綁定到viewmodel上的usercontrol屬性的內容控件。然後我使用命令參數按鈕中的ICommand來決定加載哪個用戶控件。 – 2013-03-12 19:41:09
我的所有觀點確實都是頁面。但我不熟悉usercontrol – Landvis 2013-03-13 09:00:29
使用usercontrol確實是有用的,我只是改變了我的思維方式。而不是改變紅色/灰色的內容,我現在將我的網頁的其餘部分放在用戶內容中。謝謝 – Landvis 2013-03-13 14:35:16