Click here to Skip to main content
15,904,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,
I have a question. I have a user object with properties. I have a login screen and when the user logins correctly I pass the user object to the next window. On the top of this next window I would like to display this below with the new line in the control. Should I use a text block or maybe 2 labels?
I can get this working no problem using user.Username in the control but I was wondering should I be doing this with Data Binding because I am using a WPF C# window. Thank you.
‘Hello:
username’

C#
//Login window which passes the user object to the next window
try{
                    loginUser = SQLuserAccess.UserLogin(txtUsername.Text, txtPassword.Password.ToString());
                    if (txtUsername.Text == loginUser.Username && txtPassword.Password == loginUser.Password) {
                        if (loginUser.IsAdmin == true) {
                            Window showAdminSrc = new AdminWindow(loginUser);
                            showAdminSrc.Show();
                            Close();
                        }  else if (loginUser.IsAdmin == false){
                            Window nonAdmin = new CustomerScreen(loginUser);
                            nonAdmin.Show();
                            Close();
                        } else
                            lblInvalidText.Content = "Admin is not True or False!";
                    }
//Adminwindow screen which takes object
        public AdminWindow(User user) {
            adminUser = user;
            InitializeComponent();
            //Display specific username on window.
            TextBlockName.Text = "Hello:" + Environment.NewLine +user.Username;
        }
//The control in .xaml        <TextBlock x:Name="TextBlockName" TextWrapping="Wrap" HorizontalAlignment="Right" Margin="0,30,35,0" FontSize="16">Hello:<!--<LineBreak /><Run Text="{Binding Username}"/>--></TextBlock>
//AND HERE IS SOMETHING I WAS TRYING DATA BINDING BUT HAVE NOT FIGURED OUT      		  <!--   LEARN DATA BINDING TEXTBLOCK TO DISPLAY USER OBJECT NAME.
        <TextBlock x:Name="TextBlockUsername" Text="{Binding Username}" HorizontalAlignment="Right" FontSize="16" Margin="0,60,35,0"/>
-->


What I have tried:

I have it working by passing in an object and using object.property but should i be doing this with data binding because this is WPF.
Posted
Updated 7-Nov-18 23:23pm

should i be doing this with data binding because this is WPF?

Yes.
 
Share this answer
 
1. Choice between Label and Textblock depends on requirements. If your data is only text, go for TextBlock. As it is light weight, but if you need some content like image, grid or other complex typed, then label is fine for you. Personally for simple messages, I use textblock with some styling if needed.

2. DataBinding is best choice, WPF is incompleted without MVVM. If you are not using MVVM, its possible to move your logic to viewModel. Secondly you dont need to be worry about get/set. WPF databind doing this better for you. By use of Databinding you can bind your pages to different viewModels, let say one viewModel for design time, with other for real implemenation
 
Share this answer
 

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