C
C#11mo ago
Snitrxm

❔ WPF Change Views

Im trying to switch views with MVVM pattern
49 Replies
Snitrxm
Snitrxm11mo ago
I created a NavigationStore to save the currentViewModel the problem is
public partial class MainViewModel : ViewModelBase
{
private readonly NavigationStore _navigationStore;

public ViewModelBase CurrentViewModel => _navigationStore.CurrentViewModel;

public MainViewModel(NavigationStore navigationStore)
{
_navigationStore = navigationStore;
}

public void OnCurrentViewModelChanged()
{
System.Diagnostics.Debug.WriteLine(CurrentViewModel); //ViewModels.AccountViewModel
OnPropertyChanged(nameof(CurrentViewModel));
}
}
public partial class MainViewModel : ViewModelBase
{
private readonly NavigationStore _navigationStore;

public ViewModelBase CurrentViewModel => _navigationStore.CurrentViewModel;

public MainViewModel(NavigationStore navigationStore)
{
_navigationStore = navigationStore;
}

public void OnCurrentViewModelChanged()
{
System.Diagnostics.Debug.WriteLine(CurrentViewModel); //ViewModels.AccountViewModel
OnPropertyChanged(nameof(CurrentViewModel));
}
}
Everything works fine until this part When the navigationStore calls the OnCurrentViewModelChanged() function The logs is right, shows the new view to be displayed but the function OnPropertyChanged does not make any change
Snitrxm
Snitrxm11mo ago
Snitrxm
Snitrxm11mo ago
Im binding this way
Pobiega
Pobiega11mo ago
your code shown above doesnt include any OnPropertyChanged listeners nvm, hidden in the baseclass ofc
Snitrxm
Snitrxm11mo ago
public class ViewModelBase : ObservableObject
{
}
public class ViewModelBase : ObservableObject
{
}
my base class extend the mvvm toolkit that already have the listener for OnPropertyChanged
br4kejet
br4kejet11mo ago
what about if you try binding directly to the _navigationStore's CurrentViewModel? you'd have to make it public obviously...
Snitrxm
Snitrxm11mo ago
what do you mean? ohh nevermind i got i think i cant do it, cuz my dataContext is set to the mainViewModel not to NavigationStore
br4kejet
br4kejet11mo ago
public partial class MainViewModel : ViewModelBase
{
public NavigationStore NavigationStore { get; }

public MainViewModel(NavigationStore navigationStore)
{
NavigationStore = navigationStore;
}
}
public partial class MainViewModel : ViewModelBase
{
public NavigationStore NavigationStore { get; }

public MainViewModel(NavigationStore navigationStore)
{
NavigationStore = navigationStore;
}
}
and then
Pobiega
Pobiega11mo ago
but mainViewModel has a navigation store in it.
br4kejet
br4kejet11mo ago
{Binding NavigationStore.CurrentViewModel} If that doesn't work then try putting those data templates into the ContentControl's resources. Maybe it isn't finding them in your window for some reason?
Snitrxm
Snitrxm11mo ago
when i initialize its blank now
br4kejet
br4kejet11mo ago
In your window put d:DataContext="{d:DesignInstance MainViewModel}" so that you can see any binding errors then you won't get those 3 dotted lines under some binding expressions ... unless the binding is invalid
Snitrxm
Snitrxm11mo ago
<Grid>
<ContentControl Content="{Binding NavigationStore.CurrentViewModel}" />
</Grid>
<Grid>
<ContentControl Content="{Binding NavigationStore.CurrentViewModel}" />
</Grid>
It works the same ways as before okk
br4kejet
br4kejet11mo ago
Also try moving those data templates into the ContentControl's resources instead it might not be finding the windows' resources for some reason If that still doesn't work then maybe you aren't even setting the windows' data context somehow?
Snitrxm
Snitrxm11mo ago
sry, but i dont get it, what do u mean with ContentCOntrol's resources, im kinda new in wpf
br4kejet
br4kejet11mo ago
In your window you have Window.Resources, and anything inside that, are resources that the window and any of its children have access too
Snitrxm
Snitrxm11mo ago
I do
br4kejet
br4kejet11mo ago
But the ContentControl might not be able to access them for some reason, so replace the content control with
<ContentControl Content="{Binding NavigationStore.CurrentViewModel}">
<ContentControl.Resources>
... data templates here
</ContentControl.Resources>
</ContentControl>
<ContentControl Content="{Binding NavigationStore.CurrentViewModel}">
<ContentControl.Resources>
... data templates here
</ContentControl.Resources>
</ContentControl>
If that also doesn't work then i'm not sure based on the code you provided
Snitrxm
Snitrxm11mo ago
yeah doesnt work
br4kejet
br4kejet11mo ago
Unless the DateTemplate types don't match up with the type that CurrentViewModel becomes that could also be a problem
Snitrxm
Snitrxm11mo ago
as my experience, i thinks its a update in main windows problem
public void OnCurrentViewModelChanged()
{
System.Diagnostics.Debug.WriteLine(CurrentViewModel); //ViewModels.AccountViewModel
OnPropertyChanged(nameof(CurrentViewModel));
}
public void OnCurrentViewModelChanged()
{
System.Diagnostics.Debug.WriteLine(CurrentViewModel); //ViewModels.AccountViewModel
OnPropertyChanged(nameof(CurrentViewModel));
}
as I said this functions is called when a ViewModel is changed but the new viewModels does not update in .xaml
public class NavigationStore
{
public event Action CurrentViewModelChanged;
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel
{
get => _currentViewModel;
set
{
_currentViewModel = value;
MainViewModel M = new(this);
M.OnCurrentViewModelChanged();
}
}
}
public class NavigationStore
{
public event Action CurrentViewModelChanged;
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel
{
get => _currentViewModel;
set
{
_currentViewModel = value;
MainViewModel M = new(this);
M.OnCurrentViewModelChanged();
}
}
}
this is my class NavigationStore
br4kejet
br4kejet11mo ago
Try changing ContentControl to ContentPresenter
Snitrxm
Snitrxm11mo ago
I know that calling the function this way its bad I tried to do it with an event
br4kejet
br4kejet11mo ago
I looked at one of my old projects and it uses ContentPresenter, but rarely ContentControl
Snitrxm
Snitrxm11mo ago
but does not work ok doesnt work why its soo difficult to switching between views using mvvm pattern?
br4kejet
br4kejet11mo ago
In your xaml are you still binding to CurrentViewModel or to NavigationStore.CurrentViewModel?
Snitrxm
Snitrxm11mo ago
NavigationStore.CurrentViewModel
br4kejet
br4kejet11mo ago
and you're using the MainViewModel code i provided?
Snitrxm
Snitrxm11mo ago
yes
public partial class MainViewModel : ViewModelBase
{
public NavigationStore NavigationStore { get; }

public ViewModelBase CurrentViewModel => NavigationStore.CurrentViewModel;

public MainViewModel(NavigationStore navigationStore)
{
NavigationStore = navigationStore;
}

public void OnCurrentViewModelChanged()
{
System.Diagnostics.Debug.WriteLine(CurrentViewModel); //ViewModels.AccountViewModel
OnPropertyChanged(nameof(CurrentViewModel));
}
}
public partial class MainViewModel : ViewModelBase
{
public NavigationStore NavigationStore { get; }

public ViewModelBase CurrentViewModel => NavigationStore.CurrentViewModel;

public MainViewModel(NavigationStore navigationStore)
{
NavigationStore = navigationStore;
}

public void OnCurrentViewModelChanged()
{
System.Diagnostics.Debug.WriteLine(CurrentViewModel); //ViewModels.AccountViewModel
OnPropertyChanged(nameof(CurrentViewModel));
}
}
br4kejet
br4kejet11mo ago
It's not working because you're creating a new instance of your MainViewModel each time the CurrentViewModel changes
public class NavigationStore
{
public event Action CurrentViewModelChanged;
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel
{
get => _currentViewModel;
set
{
_currentViewModel = value;
OnPropertyChanged(nameof(CurrentViewModel));
}
}
}
public class NavigationStore
{
public event Action CurrentViewModelChanged;
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel
{
get => _currentViewModel;
set
{
_currentViewModel = value;
OnPropertyChanged(nameof(CurrentViewModel));
}
}
}
if you use that instead, then it should work as long as your windows' data context is set to an instance of MainViewModel
Snitrxm
Snitrxm11mo ago
does not work
public class NavigationStore : ViewModelBase
{
public event Action CurrentViewModelChanged;
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel
{
get => _currentViewModel;
set
{
_currentViewModel = value;
OnPropertyChanged(nameof(CurrentViewModel));
}
}
}
public class NavigationStore : ViewModelBase
{
public event Action CurrentViewModelChanged;
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel
{
get => _currentViewModel;
set
{
_currentViewModel = value;
OnPropertyChanged(nameof(CurrentViewModel));
}
}
}
i also added the
ViewModelbase
ViewModelbase
br4kejet
br4kejet11mo ago
Are you sure your windows' DataContext is set to an instance of MainViewModel?
Snitrxm
Snitrxm11mo ago
yeah
public partial class MainWindow : Window
{
NavigationStore navigationStore = new();

public MainWindow()
{
InitializeComponent();
navigationStore.CurrentViewModel = new HomeViewModel(navigationStore);
DataContext = new MainViewModel(navigationStore);
}
}
public partial class MainWindow : Window
{
NavigationStore navigationStore = new();

public MainWindow()
{
InitializeComponent();
navigationStore.CurrentViewModel = new HomeViewModel(navigationStore);
DataContext = new MainViewModel(navigationStore);
}
}
br4kejet
br4kejet11mo ago
I dunno then... something must be wrong with your WPF install if binding doesn't work
Snitrxm
Snitrxm11mo ago
do you have tutorial i that i can follow?
br4kejet
br4kejet11mo ago
not really...
Snitrxm
Snitrxm11mo ago
i tried a lot tutorials in yt nothin works
br4kejet
br4kejet11mo ago
is your project on github?
Snitrxm
Snitrxm11mo ago
i can put it to you take a look
br4kejet
br4kejet11mo ago
if you do that then i can look
Snitrxm
Snitrxm11mo ago
hold on a sec please
Snitrxm
Snitrxm11mo ago
GitHub
GitHub - Snitrxm/ATM-System-MVVM
Contribute to Snitrxm/ATM-System-MVVM development by creating an account on GitHub.
Snitrxm
Snitrxm11mo ago
here u go i'll appreciated a lot if you can take a look for me
br4kejet
br4kejet11mo ago
In your HomeView and AccountView classes, remove the code where you're setting DataContext because the data context will already be set to HomeViewModel and AccountViewModel automaticall, because of how data templates work and then it should work after that
Snitrxm
Snitrxm11mo ago
yeah its works i dunno why
br4kejet
br4kejet11mo ago
wpf is spooky
Snitrxm
Snitrxm11mo ago
thx a lot bro
br4kejet
br4kejet11mo ago
np
Accord
Accord11mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Executing a method every n seconds but only during certain conditionsWhat would be the best way to do this? The easiest way I can think of would be: ```cs Task.Run(MainA❔ GtkSharp: Creating a GClosureSimply put, I am trying to complete the following code: ```csharp AccelGroup binds = new AccelGroup(✅ Confused by constant errors regarding class extension.Hi all, for my uni course, I need to code a game. I have chosen TicTacToe, but it needs to use diffe❔ How would you embed a Git commit hash in an assembly?I want to stamp a commit hash into a binary so I can display it in the UI. I'm fine with pulling stu❔ Is it possible to run integration tests with typescript as client and .net api as server?My hope is to test a client written in typescript which calls a .net api in the same solution.✅ .csproj – how to generate nodes dynamically by iterating over a string array ?how can i generate xml code in the .csproj file? i dont want to repeat my actions 10000 times... so ❔ AWS multiple file upload Using API in Razor PageI am trying to send the API request from the razor page. The Endpoint is not hitting the API Control✅ Override (abstract) getter-only property with a setterHello! I was wondering if there is anyway to add a setter to an overridden getter-only property? Cu❔ Proper way to do something in C# (migrating an old Visual Basic library)So I'm rewriting an ancient library and used an automatic transpiler to convert it all from VB.NET t❔ Getting information from web apiSo I created my own web api and am creating a console application that can consume the api. I'm curr