简体   繁体   中英

How do I correctly bind my Textbox using MVVM

I am moving my project into MVVM pattern, But the binding does not work. The below is my code of my project. The problem is the Textbox does not get updated with value when a new object is created in LRViewModel.cs but the Message Box gets poppedup, specifying that property is changed, but that does not update the textbox.

LoginRegister.xaml.cs:

using CSMS_MVVM.ViewModels;
private LrViewModel _lrViewModel;
public LoginRegister()
        {
            InitializeComponent();
        }
private void Page_Loaded_1(object sender, RoutedEventArgs e)
        {
            _lrViewModel = new LrViewModel();
            this.DataContext = _lrViewModel;
        }

LoginRegister.xaml

.
.
.
<TextBox Name="regID" Text="{Binding RegId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Padding="8" Margin="15,110,20,0" VerticalAlignment="Top" BorderBrush="#FFD4F1FF" FontSize="15" ToolTip="Username or Employee ID" FontFamily="Calibri" />
.
.
.

LRViewModel.cs

using CSMS_MVVM.Models;

namespace CSMS_MVVM.ViewModels
{
    class LrViewModel
    {
        public LrModel LrModel { get; set; }

        public LrViewModel()
        {
            LrModel=new LrModel
            {
                RegId = "Value"
            };
        }
    }
}

LrModel.cs

namespace CSMS_MVVM.Models
{
    class LrModel : INotifyPropertyChanged
    {
        private String _regId;

        public String RegId
        {
            get { return _regId; }
            set
            {
                _regId = value;
                OnPropertyChanged(new PropertyChangedEventArgs("RegId"));
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs propertyChangedEventArgs)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, propertyChangedEventArgs);
            MessageBox.Show("Here");
        }

        #endregion

    }
}

you should change to this..

<TextBox Name="regID" Text="{Binding LrModel.RegId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Padding="8" Margin="15,110,20,0" VerticalAlignment="Top" BorderBrush="#FFD4F1FF" FontSize="15" ToolTip="Username or Employee ID" FontFamily="Calibri" />

.

since the property that you want to bind to is actually a property of model, and model is a property on your view model.. you should bind to Model.PropertyName.. and when model.Property fires its property changed.. UI will be updated.

-- Suggestion --

You should never set the DataContext in your back end C# code. Instead, (in this scenario) you can set the DataContext from within the XAML.

<Window
    xmlns:ViewModels="clr-namespace:TheNamespaceWhereYourViewModelIs"
    ...
    >
   <Window.DataContext>
      <ViewModels:YourViewModel/>
   </Window.DataContext>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM