0
votes

Im working on a WPF project and use MVVM light. The textbox binding seem to not work. Do you have any idea why ? I use the INotifyPorpertyChanged implementation through the ViewModelBase Class implemented in the MVVM light framework.

I have 2 textboxes Username and Password...and i would like that properties linked to these controls have their value changed when the user enters characters in both textfields.

Thank you.

ViewModel

  #region Helpers Properties

        private UserBLL UserBLLManager;
        public UserBO User;
        public RelayCommand AddNewUserCommand { get; private set; }
        public RelayCommand TryToLoginCommand { get; private set; }

        #endregion

        public MainViewModel()
        {
            if (UserBLLManager == null)//service
                UserBLLManager = new UserBLL();

            if (User == null)//model
                User = new UserBO();

            AddNewUserCommand = new RelayCommand(() => AddNewUser());
            TryToLoginCommand = new RelayCommand(() => TryToLogin());
        }


            public void AddNewUser()
            {
                 //do stuff

            }

        }
    }

Model

public class UserBO:ObservableObject
        {   
            private string username;
            public string Username
            {
                get { return username; }
                set
                {
                    if (value != username)
                    {
                        username = value;
                        RaisePropertyChanged("Username");
                    }
                }
            }

            private string password;
            public string Password
            {
                get { return password; }
                set
                {
                    if (value != password)
                    {
                        password = value;
                        RaisePropertyChanged("Password");
                    }
                }
            }   
        }

ServiceLocator

    public class ViewModelLocator
    {

        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);


            SimpleIoc.Default.Register<MainViewModel>();

        }

        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

        public static void Cleanup()
        {
            // TODO Clear the ViewModels
        }
}

View

    <Window x:Class="MyCook.MainView"
                DataContext="{Binding Main, Source={StaticResource Locator}}">

            <Grid>

                        <TextBox Name="Username_TxtBox" Text="{Binding User.Username,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox>

                        <TextBox Name="Pwd_TxtBox" Text="{Binding User.Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ></TextBox>

</Grid>
1

1 Answers

1
votes

1) The user should be public. Binding to private properties makes no sense.

2) If you want the underlying properties to change once the users enters values you have to set the binding to TwoWay:

Text="{Binding User.Username,Mode=TwoWay}"

3) Entity classes should not derive from ViewModelBase. Instead go for ObservableObject.