用WinRT中的按钮将视图的一部分链接到不同的视图模型

问题描述:

我想在使用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

尝试这样的事情,用结合的控件属性上的视图模型内容控制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更改/设置为新的。您可以根据需要在页面上添加尽可能多的内容控件。