我有以下问题:
只显示了一个View
,其他View
没有显示,而是显示了他们的ViewModel
,尽管我声明了一些应该替换每个DataTemplate
的{{1}} ViewModel
1}} - 与它相对应的实例View
。
我该如何解决这个问题?
这是我的App.xaml
:
<Application x:Class="ThisProgram.App" 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" d1p1:Ignorable="d"
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:ThisProgram.ViewModel"
xmlns:v="clr-namespace:ThisProgram.View">
<Application.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type vm:PlayerViewModel}">
<v:PlayerView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:OperationViewModel}">
<v:OperationView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ChatBoxViewModel}">
<v:ChatBoxView/>
</DataTemplate>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Orange.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseDark.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
编辑:添加更多来源
那是我的App.xaml.cs
:
/// <summary>
/// Interaktionslogik für "App.xaml"
/// </summary>
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// Instanziieren des MainWindows
MainWindow mainWindow = new MainWindow();
// Damit wird MainWindow eine Instanz zugewisen, dessen Properties
// es sich binden kann
mainWindow.DataContext = MainWindowViewModel.Instance;
mainWindow.Show();
}
}
MainWindowViewModel.cs
:
public class MainWindowViewModel : ViewModelBase
{
private readonly PlayerRepository _playerRepository;
private readonly OperationRepository _operationRepository;
private static readonly MainWindowViewModel _instance = new MainWindowViewModel();
private readonly ObservableCollection<ViewModelBase> _viewModels = new ObservableCollection<ViewModelBase>();
public static MainWindowViewModel Instance { get { return _instance; } }
public ObservableCollection<ViewModelBase> ViewModels { get { return _viewModels; } }
private MainWindowViewModel()
{
_playerRepository = new PlayerRepository();
_operationRepository = new OperationRepository();
PlayerViewModel playerViewModel = new PlayerViewModel(_playerRepository);
OperationViewModel operationViewModel = new OperationViewModel(_operationRepository);
ChatBoxViewModel chatBoxViewModel = new ChatBoxViewModel();
this.ViewModels.Add(playerViewModel);
this.ViewModels.Add(operationViewModel);
this.ViewModels.Add(chatBoxViewModel);
}
}