2015-04-02 98 views
0

我有一个WPF应用程序,如下所示: enter image description here 当用户单击左侧(导航栏)例如服务器按钮时,它应该显示到服务器usercontrol。它应该显示右侧的不同屏幕取决于用户点击哪个按钮。 主要XAML文件代码:命令不显示正确的usercontrol

<igWpf:XamRibbonWindow x:Class="BackupCustomizing.MainWindow" 
     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" 
     xmlns:ignore="http://www.ignore.com" 
     xmlns:ig="http://schemas.infragistics.com/xaml" 
     xmlns:views="clr-namespace:BackupCustomizing.Views" 
     xmlns:viewmodel="clr-namespace:BackupCustomizing.ViewModel"    
     xmlns:igWpf="http://schemas.infragistics.com/xaml/wpf" 
     mc:Ignorable="d ignore" 
     Height="400" 
     Width="700" 
     Title="Backup customizing V0.1" 
     DataContext="{Binding Main, Source={StaticResource Locator}}" ResizeMode="NoResize"> 

    <igWpf:XamRibbonWindow.Resources> 
     <DataTemplate DataType="{x:Type viewmodel:ServerViewModel}"> 
      <views:ServerView/> 
     </DataTemplate> 
    </igWpf:XamRibbonWindow.Resources> 

    <ig:ThemeManager.Theme> 
     <ig:Office2013Theme /> 
    </ig:ThemeManager.Theme> 
    <igWpf:RibbonWindowContentHost x:Name="_content" 
            Theme="Office2013" 
            igWpf:RibbonWindowContentHost.ApplicationAccentColor="#0072C6"> 
     <Grid x:Name="LayoutRoot"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="200"/> 
       <ColumnDefinition Width="*"/> 
      </Grid.ColumnDefinitions> 
      <views:NavigationView Grid.Column="0"/> 
      <ContentPresenter Content="{Binding CurrentViewModel}" Grid.Column="1"/> 
     </Grid> 

    </igWpf:RibbonWindowContentHost> 
</igWpf:XamRibbonWindow> 

正如你可以在上面看到,我有一个参考的数据类型ServerViewModel

<igWpf:XamRibbonWindow.Resources> 
     <DataTemplate DataType="{x:Type viewmodel:ServerViewModel}"> 
      <views:ServerView/> 
     </DataTemplate> 
</igWpf:XamRibbonWindow.Resources> 

的内容演示应该显示在右侧的当前用户控件一个DataTemplate取决于哪个按钮被按下。

<ContentPresenter Content="{Binding CurrentViewModel}" Grid.Column="1"/> 

而后面的代码:

using System.Diagnostics; 
using BackupCustomizing.Model; 
using GalaSoft.MvvmLight; 
using GalaSoft.MvvmLight.Command; 

namespace BackupCustomizing.ViewModel 
{ 
    /// <summary> 
    ///  This class contains properties that the main View can data bind to. 
    ///  <para> 
    ///   See http://www.galasoft.ch/mvvm 
    ///  </para> 
    /// </summary> 
    public class MainViewModel : ViewModelBase 
    { 
     private readonly IDataService _dataService; 
     private ViewModelBase _currentViewModel; 
     private ServerViewModel _serverViewModel; 

     /// <summary> 
     /// Views change commands. 
     /// </summary> 
     public RelayCommand ServerCmd { get; set; } 


     public ViewModelBase CurrentViewModel 
     { 
      get { return _currentViewModel; } 
      set 
      { 
       _currentViewModel = value; 
       RaisePropertyChanged("CurrentViewModel"); 
      } 
     } 

     /// <summary> 
     ///  Initializes a new instance of the MainViewModel class. 
     /// </summary> 
     public MainViewModel(IDataService dataService) 
     { 
      _dataService = dataService; 
      _dataService.GetData(
       (item, error) => { }); 

      _serverViewModel = new ServerViewModel(); 
      //_currentViewModel = _serverViewModel; 

      ServerCmd = new RelayCommand(_SetServerView); 
     } 



     public void _SetServerView() 
     { 
      _currentViewModel = new ServerViewModel(); 
     } 
    } 
} 

我绑定的serverCmd relaycommand一个函数:

ServerCmd = new RelayCommand(_SetServerView); 

应该从服务器显示用户控件,将其设置到currentview的ServerView。

public void _SetServerView() 
     { 
      _currentViewModel = new ServerViewModel(); 
     } 

按钮服务器命令势必如下:

<Button Grid.Row="0" Content="Server" Margin="10 10 5 0" Command="{Binding ServerCmd}"/> 

问题是,当我点击了按钮server,它不会显示在服务器用户控件为什么呢?
如果我实例化的ServerView的currentview在视图模型构造,如:

public MainViewModel(IDataService dataService) 
     { 
      _dataService = dataService; 
      _dataService.GetData(
       (item, error) => { }); 

      _serverViewModel = new ServerViewModel(); 
      _currentViewModel = _serverViewModel; 

      ServerCmd = new RelayCommand(_SetServerView); 
     } 

话,就说明我的服务器用户控件: enter image description here

+0

并为您的问题尝试改变'ContentPresenter'到你的XAML中的'ContentControl'。 – Bolu 2015-04-02 10:50:38

+0

@Bolu yes导航位于左侧。 – 2015-04-02 11:16:26

+0

我把ContentPresenter改成了ContentControl,它不起作用。 – 2015-04-02 11:19:48

回答

0

现货的问题,在您的ServerCmd命令,你应该设置该属性不为了RaisePropertyChange领域,所以改变你的_SetServerView()到:

public void _SetServerView() 
{ 
    CurrentViewModel = new ServerViewModel(); 
} 
+0

@zero_coding,有没有反馈? – Bolu 2015-04-09 10:57:55