pass binding info to another window

Joined
Feb 9, 2023
Messages
16
Programming Experience
10+
Good afternoon i have a login system that does not use sql it uses a php database on my webserver,

what i am trying to sort out is logging in managed to do that fine,

but i am trying to retrieve the login information ( aka username )

from the txtbox in wpf and when user logs in it displays it on the main window,

there are a lot of tuts out there for data binding and i have got it working to a exstaint however my login window and my main window are diffent views,

and unsure of how to pass the username to the main form without using baseviewmodel twice on the same window

and wpf does not seem to like this,

its very basic what i have

Model,
Model Class:
    public class Models : ViewModelBase
    {
        private string username = "Unknown User";





        public string Username
        {
            get { return username; }
            set
            {
                username = value;
                OnPropertyChanged(nameof(Username));
            }
        }



    }

viewmodel Class
VM:
    public class ViewModelBase : INotifyPropertyChanged
    {

        Project_Alice.Model.Models elfenlied = new Model.Models();




        public event PropertyChangedEventHandler? PropertyChanged;

        protected  void OnPropertyChanged(string PropertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }


        //test
        public Model.Models elfenliedmodel { get; set; }


        public ViewModelBase()
        {

        }

mainWindow
C#:
<TextBlock Text="{Binding elfenliedmodel.Username}"/>

that works for the main window that binds however how i would go about liking it to the txtbox of loginview

login view appearance.

https://winpic.co/7ZHe1d7ff00dd.png

Code Behind for login view cant post all due to urls,
login window:
        void AllowAccess() //<== If player is allowed !!!
        {
            //if all is good you can access to Main Form !


            Color elfen = (Color)ColorConverter.ConvertFromString("#462ad8");
            MessageBoxEx.SetMessageBackground(elfen);
            MessageBoxEx.Show("Welcome " + txtUser.Text + " you can use Script Adder ( PROJECT Alice ) !!", "Project Alice", MessageBoxButton.OK, MessageBoxImage.Error);
            userloggedin = txtUser.Text;

            

           // XtraMessageBox.Show("Welcome " + txtUser.Text + " you can use Script Adder ( PROJECT Alice ) !!");
            MainView ALLOWED = new MainView();
            ALLOWED.Show();
            this.Hide();
        }


so what i am trying my best to accomplish is get the username from txtbox on a accepted login that has been done in above code

and to pass or bind this into the main window so i can display username text on the main window

any help would be much appreciated
kind regards
elfenliedtopfan5
 
You do not take data from the text box. You take data from the model. In this case, you would pass along the elfenliedmodel and give it to the your main window's view model. That view model would then take the data out of the model, and expose it to your view to bind to.
 
You do not take data from the text box. You take data from the model. In this case, you would pass along the elfenliedmodel and give it to the your main window's view model. That view model would then take the data out of the model, and expose it to your view to bind to.

sorry little new to mvvm pattern i dont get how i link the 2 together its quite frustrating
i am sorry to be a pain
 
Back
Top Bottom