Click here to Skip to main content
15,354,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add a navigation bar in side my views to move to sub view

However when i change the side menu bar selected item nothing show

here is the project sours code

https://github.com/markoshenoda/NeedHelp.git

What I have tried:

I tried dataTemplen in app.xaml

XML
<DataTemplate DataType="{x:Type vm:AddPepoleViewModel}">
                <v:AddPepoleView />
            </DataTemplate>

            <DataTemplate DataType="{x:Type vm:PepoleHomeViewModel}">
                <v:PepoleHomeView />
            </DataTemplate>

and also in the pepole view

XML
<ContentControl.Resources>
                            <DataTemplate DataType="{x:Type vm:AddPepoleViewModel}">
                                <local:AddPepoleView />
                            </DataTemplate>

                            <DataTemplate DataType="{x:Type vm:PepoleHomeViewModel}">
                                <local:PepoleHomeView />
                            </DataTemplate>
                        </ContentControl.Resources>
Posted
Updated 21-Feb-22 2:15am
v3

I had a quick look at your github code and it is incomplete. ViewModels are missing. So I could not run your code to see what you are trying to do.

Looking at the code above, you're trying to navigate by ViewModel.

Here is your code for the MainWindow:
XML
<UserControl>
    <!-- trimmed -->
    <Grid Background="{StaticResource ViewMainBackGroundBrush}">
        <Border Style="{StaticResource ViewItemBorder}">
            <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
                <TextBlock Foreground="White" Text="Main View" />
            </Grid>
        </Border>
    </Grid>
</UserControl>

You currently have not implemented the host for your ViewModels to display any views. You need to implement something like this:
XML
<Window x:Class="app.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"
        Title="page name goes here" Height="900" Width="1200">

        <ContentControl Content="{Binding CurrentViewModel}" />
</Window>

and the code behind would look something like this:
C#
public partial class MainWindow
{
    public MainWindow(IMainViewModel viewModel)
    {
        this.DataContext = viewModel;
        this.InitializeComponent();
    }
}

And your MainViewModel to change views would be something like this:
C#
public class MainViewModel : NavigationViewModelBase, IMainViewModel
{
    public MainViewModel(INavigationStore navigationStore) : base(navigationStore)
    {
    }
}


public abstract class NavigationViewModelBase : ViewModelBase
{
    private readonly INavigationStore _navigationStore;

    public ViewModelBase CurrentViewModel => this._navigationStore.CurrentViewModel;

    protected NavigationViewModelBase(INavigationStore navigationStore)
    {
        this._navigationStore = navigationStore;
        this._navigationStore.CurrentViewModelChanged += this.OnCurrentViewModelChanged;
    }

    private void OnCurrentViewModelChanged() => this.OnPropertyChanged(nameof(this.CurrentViewModel));
}

INavigationStore is just a way of passing the CurrentViewModel that you wish to display from your navigation / SideMenu.
   
v5
Comments
Member 7912784 21-Feb-22 4:36am
   
ViewModels are in it own project and it is in the project

HoursTrans.ViewMoudels
Graeme_Grant 21-Feb-22 4:44am
   
Check your github. There is a project named HoursTrans.ViewMoudels but no ViewModel classes, only Command classes and Menu classes.
Member 7912784 21-Feb-22 6:45am
   
I'm sorry about that. Uploaded
Graeme_Grant 21-Feb-22 8:18am
   
Your code is very tightly dependent. I would be very concerned with memory leaks. It looks like a binding in your sub menu is not firing correctly. Please see my comments in Solution 2.
It looks like you have a binding issue. INotifyPropertyChanged does not look like it is correctly implemented.

Your code:
public event PropertyChangedEventHandler PropertyChanged = (sendr, e) => { };

Corrent implementation:
public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    => this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

This would be a key change that I would make.
   
Comments
Member 7912784 21-Feb-22 8:23am
   
There is NuGet Package called PropertyChanged.Fody It mange all that

https://github.com/Fody/PropertyChanged

That's why Main Navigation work
Graeme_Grant 21-Feb-22 8:31am
   
You have a binding issue regardless.
Member 7912784 21-Feb-22 8:33am
   
Where is that, i can't find
Graeme_Grant 21-Feb-22 8:37am
   
<ContentControl DataContext="{Binding SideNaveer.SelectedView}">...</ContentControl> in People.xaml binding is not working.
Member 7912784 21-Feb-22 8:40am
   
Found it Thank you so much for your help
Member 7912784 21-Feb-22 8:45am
   
This is The wrong code
<contentcontrol datacontext="{Binding SideNaveer.SelectedView}">... in People.xaml binding is not working.

The fix
<contentcontrol content="{Binding SideNaveer.SelectedView}">
Member 7912784 21-Feb-22 8:47am
   
Thank you Graeme_Grant for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900