C#C
C#3y ago
Mek

✅ how to pass username?

in my HomeScreenView.axaml file I have
<TextBox Text="{Binding PlayerUserName}" />
<Button Command="{Binding $parent[Window].DataContext.NewGame}" />
When a player clicks the New Game button, how can I pass the username back to the parent file MainWindowViewModel.cs
// MainWindowViewModel.cs
namespace Phasmo_Trivia.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        ViewModelBase _content;

        public MainWindowViewModel(Database db)
        {
            Content = List = new HomeScreenViewModel(db); 
        }

        public ViewModelBase Content
        {
            get => _content;
            private set => this.RaiseAndSetIfChanged(ref _content, value);
        }

        public HomeScreenViewModel List { get; }

        public void NewGame()
        {
            var vm = NewGameViewModel();
        }
        public void PreviousGames()
        {

        }
    }
}
I need to pass this username to the NewGameViewModel(); so that as the player plays the game, it can store the username along with the game results.
Was this page helpful?