0
我在WPF项目上工作并使用MVVM光源。该文本框绑定似乎不起作用。你有什么想法,为什么?我通过在MVVM light框架中实现的ViewModelBase类使用INotifyPorpertyChanged实现。MVVM Light Binding
我有2个文本框的用户名和密码...我希望链接到这些控件的属性在用户在两个文本框中输入字符时都改变其值。
谢谢。
视图模型
#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
}
}
}
模型
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
}
}
查看
<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>
我做了你通知的改变,但它仍然不起作用(我编辑了我的代码)。我改变了我的模型类(UserBO)中的派生,如你所说... – 2015-02-11 16:33:05
它的工作原理谢谢 – 2015-02-12 11:10:43