Domanda

I'm writing a demo program using MvvmLight.What I want is binding a TextBox in the View to a property in the Model.The binding seems right,for I can show my input in the TextBox by a MessageBox.The problem is that after setting the property to string.Empty in the ViewModel,the textbox in the View remains unchanged.

Model:LoginToMisInfo.cs

using GalaSoft.MvvmLight;

namespace MvvmLight1.Model
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class LoginToMisInfo : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the LoginToMisInfo class.
        /// </summary>
        public LoginToMisInfo()
        {
        }
        private string userName;

        public string UserName
        {
            get
            {
                return userName;
            }
            set
            {
                userName = value;
            }
        }
    }
}

ViewModel:LoginToMis.cs

using GalaSoft.MvvmLight;
using MvvmLight1.Model;
using System.ComponentModel;
using GalaSoft.MvvmLight.Command;
using System.Windows.Input;
using System.Windows;

namespace MvvmLight1.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// See http://www.galasoft.ch/mvvm
    /// </para>
    /// </summary>
    public class LoginToMis : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the LoginToMis class.
        /// </summary>
        public LoginToMis()
        {
            //CommandCancel = new RelayCommand(() => ConfirmExecute(), () => true);
            CommandConfirm = new RelayCommand(() => ConfirmExecute(), () => true);
            CommandClear = new RelayCommand(() => ClearExecuate(), () => true);
        }

        LoginToMisInfo LoginInfo = new LoginToMisInfo();
        public event PropertyChangedEventHandler PropertyChanged;

        public string UserName
        {
            get
            {
                return LoginInfo.UserName;
            }
            set
            {
                LoginInfo.UserName = value;
                if(this.PropertyChanged!=null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("LoginInfo.UserName"));
                }
            }
        }

        public ICommand CommandCancel { get; private set; }
        public ICommand CommandConfirm { get; private set; }
        public ICommand CommandClear { get; private set; }

        private void ConfirmExecute()
        {
            MessageBox.Show(UserName);
        }

        private void ClearExecuate()
        {
            UserName = string.Empty;
        }
    }
}

View:

DataContext

DataContext="{Binding MyLoginToMis, Source={StaticResource Locator}

(I've registered LoginToMis in the ViewModelLocator)

TextBox

<TextBox Grid.Row="0" Grid.Column="1"
MinWidth="100" 
Text="{Binding UserName,UpdateSourceTrigger=PropertyChanged}"/>

'Button'

<Button Content="Confirm" MinWidth="150" MinHeight="80"
Margin="50,0,50,0" IsDefault="True" 
Command="{Binding CommandConfirm}"/>

<Button Content="Clear" MinWidth="150" MinHeight="80"
Margin="50,0,50,0" Command="{Binding CommandClear}"/>

So basically,I create a class in the Model,and wrap a instance of it in the ViewModel,then I bind the property in the ViewModel to the TextBox in the View.After entering some words in the TextBox and click Confirm,a MessageBox will pop up and show the inputs in the TextBox.Clicking Clear,however,the TextBox do not update.Any ideas?

È stato utile?

Soluzione

You should raise property changed notification for UserName property instead.

And you can use RaisePropertyChanged method provided by MvvmLight to raise property changed notification. The method available from ViewModelBase class which inherits ObservableObject :

RaisePropertyChanged("UserName");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top