Menu View Not Loading [duplicate] - c#

This question already has answers here:
Why does WPF support binding to properties of an object, but not fields?
(2 answers)
using of INotifyPropertyChanged
(3 answers)
Closed 11 months ago.
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?

Related

Loading page in frame using WPF. frame can redirect into page. but design not Loaded

I'm WPF developer i need to load multiple page. Some Codes try working but design not loaded
below page C# code.
page C#
public partial class Dashboard : Page
{
public Dashboard()
{
InitializeComponent();
MessageBox.Show("Working");
}
}
below page xml code
page XML
<Page x:Class="Project.View.Dashboard"
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:Project.View"
mc:Ignorable="d"
Title="Dashboard">
<Grid Background="Black" >
</Grid>
</Page>
below window c# code
Window C#
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
frame.NavigationService.Navigate(new Dashboard());
}
}
below window XML code
Window XML
<Window x:Class="Project.View.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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:Project.View"
mc:Ignorable="d" Name="MainWindow1"
Title="MainWindow" Height="768" Width="1366" WindowState="Maximized" WindowStyle="None">
<Grid>
<Frame x:Name="frame" Background="Violet" NavigationUIVisibility="Hidden" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5"/>
</Grid>
</window>

Change entire window view in wpf [duplicate]

This question already has answers here:
If the user logs on successfully, then I want to show the main window, if not, I want to exit the application
(3 answers)
Closed 4 years ago.
I have a main window that I created and it looks like usual log in screen.
Once the user click on a button I would like the whole window to change.
In all the examples I saw either I have to keep a stackpanel/dock and then only the frame changes.
For example, I have the following Main window xaml:
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Frame x:Name="Main" Margin="0,35,0,0"/>
<Button Content="Button" Click="Button_Click" Margin="0,105,326,100"/>
</Grid>
</Window>
and the new page:
<Page x:Class="WpfApp1.Optimizer"
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:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Optimizer">
<Grid Background="Black">
<Button Content="Button" HorizontalAlignment="Left" Height="91" Margin="125,87,0,0" VerticalAlignment="Top" Width="129"/>
</Grid>
</Page>
And my command is:
private void Button_Click(object sender, RoutedEventArgs e)
{
Main.Content = new Optimizer();
}
In that case the two views appear on same page instead of switching completely.
Its not the case im looking for. I need the whole window to change and not leave behind a stackpanel.
Any simple example would be appreciated.
First thing to mention here is that pages and frames are rarely used by commercial teams. At least not in my experience.
Unless writing xbap - which is wpf in a browser.
I suggest you consider usercontrols rather than pages and host them in contentcontrols.
You can then have something like:
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Click="Button_Click"/>
</Grid>
</Window>
Click handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Content = new UserControl1();
}
That makes the entire content of mainwindow an instance of UserControl1.
A Window inherits from contentcontrol.
You should also have learning mvvm on your list.
You should learn that. Maybe not right now, but soon.

C# WPF UserControls are not in the windows

the MainWindow Code is like this:
<Window x:Class="UserGUI.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:UserGUI"
mc:Ignorable="d"
Title="MainWindow" Height="657.278" Width="952.33" ResizeMode="CanMinimize">
<Canvas>
<ContentControl Name="mainContentControl" Grid.Column="0" Height="585" Width="934" Canvas.Top="31"/>
<ContentControl Name="upperMenu" Grid.Column="1" Height="31" Width="944"/>
</Canvas>
and the xaml is:
public MainWindow()
{
InitializeComponent();
mainContentControl.Content = new AdminMenu();
upperMenu.Content = new LoginScreen();
}
But I do not see the user controls in my mainwindow...
It seems they are not aligned at all
What am i doing wrong?
The LoginScreen Canvas is:
<Canvas x:Name="loginCanvas" Background="BlanchedAlmond" Height="620" VerticalAlignment="Top" HorizontalAlignment="Left" Width="934">
and the window of the userControl window itself is:
Height="630" Width="944">
Hope i'm clear enough
There are some strange things in your XAML. There is no Grid and no columns, but you have some positioning based on it. Try this:
<Window x:Class="UserGUI.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:local="clr-namespace:UserGUI"
Title="MainWindow" Height="657.278" Width="952.33" ResizeMode="CanMinimize">
<Canvas>
<ContentControl Name="mainContentControl" Height="585" Width="934" Canvas.Top="31" Canvas.Left="0"/>
<ContentControl Name="upperMenu" Canvas.Top="0" Canvas.Left="0" Height="31" Width="944"/>
</Canvas>

Showing a control based on type of databound viewmodel?

I'm learning WPF at the moment. I'm finding xaml quite tough to use. I have MainWindow.xaml defined like this:
<Window x:Class="Compliance.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary Source="MainWindow.Resources.xaml"></ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</Window>
And MainWindow.Resources.xaml like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:Compliance.ViewModel"
xmlns:vw="clr-namespace:Compliance.View">
<DataTemplate DataType="{x:Type vm:Entities.AbstractEntityViewModel}">
<vw:AbstractEntityView></vw:AbstractEntityView>
</DataTemplate>
</ResourceDictionary>
AbstractEntityView is like this:
<UserControl x:Class="Compliance.View.AbstractEntityView"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel>
<Label Content="ID:"></Label>
<TextBlock Text="{Binding Path=EntityId}"></TextBlock>
</StackPanel>
</UserControl>
Then in App.xaml.cs I am overriding OnStartup like this:
MainWindow window = new MainWindow();
//Model class
Individual ind = new Individual(1,"Name");
//subclass of AbstractEntityViewModel
var vm = new Entities.IndividualEntityViewModel(ind);
window.DataContext = vm;
window.Show();
However, nothing appears in the window.
I used the answer from this question to get my control to render. However, this requires you to refer to elements in the view from the code, which I don't want to do.
Is it possible to get a window to pick a View to render based on the ViewModel set as its datacontext? Or do I have the wrong idea about how MVVM is supposed to work?
You have the right idea, but you're not actually telling WPF to display your ViewModel anywhere
I usually host a ViewModel in a ContentControl object if I am binding to a single ViewModel
<Window x:Class="Compliance.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary Source="MainWindow.Resources.xaml"></ResourceDictionary>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding }" />
</Grid>
</Window>
The ContentControl is usually not needed for lists of Models or ViewModels, since the object is automatically inserted as the Content property of the ContentPresenter of each item. For example, no ContentControl is needed when binding a ListBox to a collection of ViewModels
<ListBox ItemsSource="{Binding MyCollectionOfViewModel}" />

How to instantiate DataContext object in XAML

I want to be able to create an instance of the DataContext object for my WPF StartupUri window in XAML, as opposed to creating it code and then setting the DataContext property programmaticly.
The main reason is I don't need to access the object created externally and I don't want to have to write code behind just for setting the DataContext.
I'm sure I've read somewhere how to instantiate the DataContext object in XAML but I can't find it in any of the usual places...
You add an XML namespace for whatever namespace your DataContext lives in, create an instance of it in the Window Resources and set the DataContext to that resource:
<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<local:MyViewModel x:Key="MyViewModel"/>
</Window.Resources>
<Grid DataContext="{StaticResource MyViewModel}">
</Grid>
</Window>
You can just specify this directly in XAML for the entire Window:
<Window
... xmlns definitions ...
>
<Window.DataContext>
<local:CustomViewModel />
</Window.DataContext>
</Window>
This creates a view model named "CustomViewModel" in the namespace aliased to local, directly as the DataContext for the Window.
Assuming this code:
public abstract class BaseView { }
public class RuntimeView : BaseView { }
public class DesigntimeView : BaseView { }
Try this:
<Page.DataContext>
<local:RuntimeView />
</Page.DataContext>
<d:Page.DataContext>
<local:DesigntimeView />
</d:Page.DataContext>
<ListBox ItemsSource="{Binding}" />
Best of luck!
If you need to set the DataContext as same control class:
<Window x:Class="TabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TabControl"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
>
</Window>
use RelativeSource binding.
or just
<Window x:Class="TabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TabControl"
Title="MainWindow" Height="350" Width="525"
>
<Window.DataContext>
< new instance of any viewModel here....>
</Window.DataContext>
</Window>
If want to assign an instance of different class than itself.

Categories

Resources