2013-03-11 18 views
3

我想在使用MVVMlight框架的winRT中創建一個程序。在應用程序中,我有一個應該保持不變的部分,以及應該將其內容鏈接到特定視圖模型的部分。我將在下面提供的正是我想說的一個小例子:用WinRT中的按鈕將視圖的一部分鏈接到不同的視圖模型

enter image description here

所以當我按下灰色的按鈕,內容應該是灰色的,當我按下紅色按鈕的內容應該是紅色的,但是該頁面的其餘部分應該保持不變。

我現在能想到的唯一方法是在我的視圖中放置多個數據模板,並且只在我需要它們時填充它們綁定的列表,以便在清除時填充並消失時顯示它們,但我認爲這樣做會使視圖有點混亂,我想知道是否沒有任何其他方式來做到這一點?

我真的很想實現的是,當我點擊一個按鈕(灰色或紅色),將有一個相應的視圖模型,將被加載到contentarea,contentarea beying紅色/灰色atm。

它應該是像我在本教程中找到的,但對於WinRt,因爲​​我不能讓本教程在WinRt中工作。

http://www.codeproject.com/Articles/323187/MVVMLight-Using-Two-Views

+2

你的觀點是什麼,他們是頁面,用戶控件等?我使用綁定到viewmodel上的usercontrol屬性的內容控件。然後我使用命令參數按鈕中的ICommand來決定加載哪個用戶控件。 – 2013-03-12 19:41:09

+0

我的所有觀點確實都是頁面。但我不熟悉usercontrol – Landvis 2013-03-13 09:00:29

+0

使用usercontrol確實是有用的,我只是改變了我的思維方式。而不是改變紅色/灰色的內容,我現在將我的網頁的其餘部分放在用戶內容中。謝謝 – Landvis 2013-03-13 14:35:16

回答

2

嘗試這樣的事情,用結合的控件屬性上的視圖模型內容控制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更改/設置爲新的。您可以根據需要在頁面上添加儘可能多的內容控件。

相關問題