I am beginner and have an issue in setting a WPF project and following MVVM pattern; I do not see how to link the view to the viewmodel with the organization below :
I have set 3 folders : Model, View and ViewModel, both at the root of the project named "Company.App.UI". The App.xaml and MainWindow.xaml are at the root of the project.
Starting with this, I want control the content displayed in the client area of the MainWindow by : - having the rendered views in the folder 'View' as UserControls, for example 'LoginView.xaml' - having the corresponding view model in the folder 'ViewModel', for example 'LoginView.xaml.cs'
Then what I did in MainWindow.xaml is :
<Window x:Class="Company.App.UI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewmodel="clr-namespace:Company.App.UI.ViewModel"
xmlns:view="clr-namespace:Company.App.UI.View" <!-- does not work, not a namespace -->
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type viewmodel:LoginViewModel}">
<view:LoginView/> <!-- does not work -->
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<ContentControl Content="{Binding ClientArea}"/>
</StackPanel>
</Grid>
</Window>
And in MainWindow.xaml.cs :
using System.Windows;
using System.Windows.Controls;
using Company.App.UI.ViewModel;
namespace Company.App.UI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private UserControl _ClientArea = null;
public UserControl ClientArea
{
get { return _ClientArea; }
set { _ClientArea = value; }
}
public MainWindow()
{
if (_ClientArea == null) { ClientArea = new LoginViewModel(); }
InitializeComponent();
}
}
}
The LoginView is a simple UserControl with one Label just to see it is what it is. If I put my LoginView.xaml at the root of the project, next to MainWindow.xaml, it works ... What am I doing wrong / missing ? I do not want to use any frameworks (PRISM and so on) for getting this to work. My apologies if my post is a duplicate but I have also fail to find it while searching. Thanks,
Update
I use VS2013 with 0 updates / patches / etc. Everything is in the same project.
The errors output is :
- The type or namespace name 'View' does not exist in the namespace 'Company.App.UI' (are you missing an assembly reference?)
- The name "LoginView" does not exist in the namespace "clr-namespace:Company.App.UI.View".
- The type 'view:LoginView' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
LoginView.xaml :
<UserControl x:Class="Company.App.UI.ViewModel.LoginViewModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Label>User control login</Label>
</Grid>
</UserControl>
LoginViewModel.cs :
using System.Windows.Controls;
namespace Company.App.UI.ViewModel
{
public partial class LoginViewModel : UserControl
{
public LoginViewModel()
{
}
}
}