【发布时间】:2015-04-02 10:26:35
【问题描述】:
我有一个 WPF 应用程序,如下所示: 当用户单击左侧(导航栏)例如服务器按钮时,它应该显示给服务器用户控件。它应该在右侧显示不同的屏幕,这取决于用户单击了哪个按钮。 主要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 中继命令绑定到一个函数:
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时,为什么不显示服务器用户控件?
如果我在 viewmodel 构造函数中使用 serverview 实例化当前视图,例如:
public MainViewModel(IDataService dataService)
{
_dataService = dataService;
_dataService.GetData(
(item, error) => { });
_serverViewModel = new ServerViewModel();
_currentViewModel = _serverViewModel;
ServerCmd = new RelayCommand(_SetServerView);
}
然后它显示了服务器用户控件:
【问题讨论】:
-
对于您的问题,请尝试在您的 XAML 中将
ContentPresenter更改为ContentControl。 -
@Bolu 是的,导航在左侧。
-
我把
ContentPresenter改成ContentControl还是不行。
标签: c# wpf xaml user-controls