请教WPF高手关于MVVM模式里面INotifyPropertyChanged的问题。
Model有一个类Person,有属性Name,另一个类PersonViewModel,有Person类型属性P,在View的资源里面定义了PersonViewModel...
Model有一个类Person,有属性Name,另一个类PersonViewModel,有Person类型属性P,在View的资源里面定义了PersonViewModel的实例PVM、数据模板,并用ContentControl用TextBox显示PVM的P的Name。另外有一个按钮,修改PVM的P的Name。
问题:如果要实现点击按钮修改属性以后,界面上绑定的Name立即反映改变,则哪些类需要实现INotifyPropertyChanged接口?是Person还是PersonViewModel还是两者? 展开
问题:如果要实现点击按钮修改属性以后,界面上绑定的Name立即反映改变,则哪些类需要实现INotifyPropertyChanged接口?是Person还是PersonViewModel还是两者? 展开
展开全部
只要Model实现这个接口就可以了。我写了个demo
view层:(datacontext绑定到vm层)
<StackPanel Orientation="Horizontal">
<TextBox Width="100" Height="23" DataContext="{Binding Person1}" Text="{Binding PName}"/>
<Button Margin="20 0 " VerticalAlignment="Center" Content="click" Command="{Binding UpdateNameCommand}"/>
</StackPanel>
Model层:Person类实现INotifyPropertyChanged接口,有一个PName属性
private string pname;
public string PName
{
get { return pname; }
set
{
if (PropertyChanged != null)
{
pname = value;
PropertyChanged(this, new PropertyChangedEventArgs("PName"));
}
}
}
ViewModel层:
public class PersonViewModel
{
private Person person1;
public Person Person1
{
get { return person1; }
set
{
person1 = value;
}
}
public void UpdateName()
{
if (person1 != null)
{
person1.PName = "aaa";
}
}
public ICommand UpdateNameCommand { get; set; }
public PersonViewModel()
{
person1 = new Person();
UpdateNameCommand = new MyCommand(this);
}
public event PropertyChangedEventHandler PropertyChanged;
}
命令:
public class MyCommand : ICommand
{
public PersonViewModel vm = null;
public MyCommand(PersonViewModel vm)
{
this.vm = vm;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged
{
add { }
remove { }
}
public void Execute(object parameter)
{
vm.UpdateName();
}
}
view层:(datacontext绑定到vm层)
<StackPanel Orientation="Horizontal">
<TextBox Width="100" Height="23" DataContext="{Binding Person1}" Text="{Binding PName}"/>
<Button Margin="20 0 " VerticalAlignment="Center" Content="click" Command="{Binding UpdateNameCommand}"/>
</StackPanel>
Model层:Person类实现INotifyPropertyChanged接口,有一个PName属性
private string pname;
public string PName
{
get { return pname; }
set
{
if (PropertyChanged != null)
{
pname = value;
PropertyChanged(this, new PropertyChangedEventArgs("PName"));
}
}
}
ViewModel层:
public class PersonViewModel
{
private Person person1;
public Person Person1
{
get { return person1; }
set
{
person1 = value;
}
}
public void UpdateName()
{
if (person1 != null)
{
person1.PName = "aaa";
}
}
public ICommand UpdateNameCommand { get; set; }
public PersonViewModel()
{
person1 = new Person();
UpdateNameCommand = new MyCommand(this);
}
public event PropertyChangedEventHandler PropertyChanged;
}
命令:
public class MyCommand : ICommand
{
public PersonViewModel vm = null;
public MyCommand(PersonViewModel vm)
{
this.vm = vm;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged
{
add { }
remove { }
}
public void Execute(object parameter)
{
vm.UpdateName();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询