我在我的视图中更新文本框文本时遇到问题。我已经看到一些其他线程,但无法找到一个接近我的设置。绑定的文本框文本不会更新
我有一类
interface IPerson : INotifyPropertyChanged
{
string FirstName { get; set;}
}
public class Person
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private string _firstName;
public string FirstName
{
get {return _firstName;}
set
{
_firstName = value;
PropertyChanged(this , new PropertChangedEventArgs("FirstName"));
}
}
}
模型这是我的视图模型看起来像
interface IViewModel : INotifyPropertyChanged
{
string FirstName { get; set;}
IPerson Person { get; set; }
}
public class viewModel : IViewModel
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private IPerson _person;
public Person Person
{
get {return _person;}
set
{
_person = value;
PropertyChanged(this , new PropertyChangedEventArgs("Person"));
}
}
public string FirstName
{
get {return Person.FirstName;}
Set
{
Person.FirstName = value;
PropertyChanged(this , new PropertChangedEventArgs("FirstName"));
}
}
}
XAML绑定设置
// If i use this binding my model will get updated but my textbox text will never show what the value is in the model
Text="{Binding Path=FirstName ,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,diag:PresentationTraceSources.TraceLevel=High}"
// This binding works fine both ways
Text="{Binding Path=Person.FirstName ,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,diag:PresentationTraceSources.TraceLevel=High}"
所以,如果我绑定到人。名字一切都很好。如果我绑定到名字,那么它会更新我的模型,但不会获取文本框文本的数据。当我在列表中选择一个项目时,这变得很明显。
有没有人有一个想法,为什么会发生这种情况。我希望能够绑定到ViewModel Firstname,并让它传递给我的person对象。
什么是您的DataContext?根据代码我认为它的viewModel。因此,如果属性在Person中更改那么如果使用Just FirstName,则viewModel clas将不会收到通知。所以尝试在viewModel中触发PropertyChanged。 –
这就是我如何执行DataContext公共EmployeeAdministrationView(IEmployeeAdministrationViewModel虚拟机):this() DataContext = vm; } –
我认为我意外地删除了某人的评论。我很抱歉。他们问我dataContext –