我有一个WPF应用程序,如下所示: 当用户单击左侧(导航栏)例如服务器按钮时,它应显示给服务器usercontrol。它应该在右侧显示不同的屏幕取决于用户点击的按钮。 主要的xaml文件代码:
<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>
应该在右侧显示当前用户控件的内容呈现器取决于按下了哪个按钮。
<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 relay命令绑定到一个函数:
ServerCmd = new RelayCommand(_SetServerView);
应显示来自服务器的usercontrol,它将currentview设置为serverview。
public void _SetServerView()
{
_currentViewModel = new ServerViewModel();
}
按钮服务器命令绑定如下:
<Button Grid.Row="0" Content="Server" Margin="10 10 5 0" Command="{Binding ServerCmd}"/>
问题是,当我点击按钮server
时,它没有显示服务器用户控件为什么?
如果我在viewmodel构造函数中使用serverview实例化当前视图,如:
public MainViewModel(IDataService dataService)
{
_dataService = dataService;
_dataService.GetData(
(item, error) => { });
_serverViewModel = new ServerViewModel();
_currentViewModel = _serverViewModel;
ServerCmd = new RelayCommand(_SetServerView);
}
然后它显示了服务器usercontrol:
答案 0 :(得分:0)
发现问题,在ServerCmd
命令中,您应该将属性设置为字段而不是RaisePropertyChange
,因此请将_SetServerView()
更改为:
public void _SetServerView()
{
CurrentViewModel = new ServerViewModel();
}