'Menu View Not Loading [duplicate]
i'm struggling to get a very simple mvvm app off the ground and not sure why, imagine an app such as the below:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow app = new MainWindow();
MainViewModel context = new MainViewModel();
app.DataContext = context;
app.Show();
}
}
And a mainViewModel that looks like this:
class MainViewModel
{
public object CurrentViewModel;
public MainViewModel()
{
CurrentViewModel = new MenuViewModel();
}
}
and the MainWindow like so:
<Window x:Class="myApp.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:local="clr-namespace:myApp"
mc:Ignorable="d"
Title="MainWindow" Height="460" Width="800">
<Window.Resources>
<DataTemplate DataType="{x:Type local:MenuViewModel}">
<local:Menu />
</DataTemplate>
</Window.Resources>
<ContentControl
Content="{Binding CurrentViewModel}"
HorizontalAlignment="Left"
VerticalAlignment="Top" Height="440" Width="790" Margin="0,0,0,-21"/>
</Window>
with the menu Page View like this:
<UserControl x:Class="myApp.Menu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:myApp"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Button Content="MyButton" HorizontalAlignment="Left" Margin="34,90,0,0"
VerticalAlignment="Top" Width="75"/>
</Grid>
</UserControl>
So basically, when the application loads i would like to show the menu View, but the code
CurrentViewModel = new MenuViewModel();
inside MainViewMode is never hit, i am assuming that is why the content control is never populated with the Menu View and why the button inside the Menu is never displayed,
i can't understand why this point is never reached, what fundamentally hilarious thing am i missing?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
