Search code examples
wpfdatastore

WPF After login,store username to another xaml


I have 2 xaml window and i want to store data between windows.How can i do?


Solution

  • Refer the below code sample for passing using constructor.

    <Window x:Class="DelegateComd_Learning.Login"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Login" Height="300" Width="300">
    <Grid>
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Username"/>
                <TextBox x:Name="txtUsername" Width="100"/>
            </StackPanel>
            <Button Content="Login" Click="Button_Click"/>
        </StackPanel>
    </Grid>
    

    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Window1 win = new Window1(txtUsername.Text);
            win.Show();
        }
    }
    <Window x:Class="DelegateComd_Learning.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBlock x:Name="txtDisplayUsername"/>
    </Grid>
    

     public partial class Window1 : Window
    {
        public Window1(string username)
        {
            InitializeComponent();
            txtDisplayUsername.Text = username;
        }
    }