Wpf 使用MVVM将datacontext绑定到用户控件
我有一个模型Wpf 使用MVVM将datacontext绑定到用户控件,wpf,wpf-controls,Wpf,Wpf Controls,我有一个模型 public class PersonModel : INotifyPropertyChanged { private string _firstname; public string FirstName { get {return _firstname; } set { _firstname = value; RaisePropertyChanged("First
public class PersonModel : INotifyPropertyChanged
{
private string _firstname;
public string FirstName
{
get {return _firstname; }
set { _firstname = value; RaisePropertyChanged("FirstName"); }
}
private string _lastname;
public string LastName
{
get { return _lastname; }
set { _lastname = value; RaisePropertyChanged("LastName"); }
}
public string FullName
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
我有一个viewmodel:
public class PersonViewModel
{
public ObservableCollection<PersonModel> Person { get; set; }
public PersonViewModel()
{
Person = CookPersonData();
}
internal static ObservableCollection<PersonModel> CookPersonData()
{
ObservableCollection<PersonModel> persons = new ObservableCollection<PersonModel>();
persons.Add(new PersonModel{ FirstName="Raj", LastName="kumar" });
return persons;
}
}
ViewModel未实现INotifyPropertyChanged,因此在设置集合时,不会将此更改通知视图
一般来说,我建议不要使用这种设置:您混合使用模型和视图模型,这将在模型的视图中创建一个依赖项。尽管它现在可能在将来的版本中工作,但它可能会成为一个问题,而且它与MVVM模式背道而驰。更改此版本中的代码:
public class PersonViewModel : INotifyPropertyChanged
{
public ObservableCollection<PersonModel> _person = new ObservableCollection<PersonModel>();
public ObservableCollection<PersonModel> Person
{
get { return _person; }
set { _person = value; RaisePropertyChanged("Person"); }
}
public PersonViewModel()
{
UpdateView = new DelegateCommand(this.OnUpdateView, this.CanUpdateView);
var tmp = CookPersonData();
Person.Clear();
foreach (var item in tmp)
{
Person.Add(item);
}
}
internal static ObservableCollection<PersonModel> CookPersonData()
{
ObservableCollection<PersonModel> persons = new ObservableCollection<PersonModel>();
persons.Add(new PersonModel { FirstName = "Raj", LastName = "kumar" });
return persons;
}
internal static ObservableCollection<PersonModel> CookPersonData2()
{
ObservableCollection<PersonModel> persons = new ObservableCollection<PersonModel>();
persons.Add(new PersonModel { FirstName = "Raj2", LastName = "kumar2" });
return persons;
}
public ICommand UpdateView { get; private set; }
private void OnUpdateView()
{
var tmp = CookPersonData2();
Person.Clear();
foreach (var item in tmp)
{
Person.Add(item);
}
}
private bool CanUpdateView()
{
return true;
}
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
和XAML文件:
<Window.DataContext>
<local:PersonViewModel />
</Window.DataContext>
<StackPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="FirstName"></TextBlock>
<TextBox Text="{Binding Person[0].FirstName,Mode=TwoWay}" Name="txtFirstName" Margin="115,0,0,106" Background="#FFF0F0F0" />
<TextBlock Grid.Row="1" Text="FirstName"></TextBlock>
<TextBox Grid.Row="1" Text="{Binding Person[0].LastName,Mode=TwoWay}" Name="txtLastName" Margin="115,0,0,110" Background="#FFF0F0F0" />
</Grid>
<Button Content="Update" Command="{Binding UpdateView}" />
</StackPanel>
谢谢,它现在起作用了,我有了更好的理解,非常感谢。
public class PersonViewModel : INotifyPropertyChanged
{
public ObservableCollection<PersonModel> _person = new ObservableCollection<PersonModel>();
public ObservableCollection<PersonModel> Person
{
get { return _person; }
set { _person = value; RaisePropertyChanged("Person"); }
}
public PersonViewModel()
{
UpdateView = new DelegateCommand(this.OnUpdateView, this.CanUpdateView);
var tmp = CookPersonData();
Person.Clear();
foreach (var item in tmp)
{
Person.Add(item);
}
}
internal static ObservableCollection<PersonModel> CookPersonData()
{
ObservableCollection<PersonModel> persons = new ObservableCollection<PersonModel>();
persons.Add(new PersonModel { FirstName = "Raj", LastName = "kumar" });
return persons;
}
internal static ObservableCollection<PersonModel> CookPersonData2()
{
ObservableCollection<PersonModel> persons = new ObservableCollection<PersonModel>();
persons.Add(new PersonModel { FirstName = "Raj2", LastName = "kumar2" });
return persons;
}
public ICommand UpdateView { get; private set; }
private void OnUpdateView()
{
var tmp = CookPersonData2();
Person.Clear();
foreach (var item in tmp)
{
Person.Add(item);
}
}
private bool CanUpdateView()
{
return true;
}
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
<Window.DataContext>
<local:PersonViewModel />
</Window.DataContext>
<StackPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="FirstName"></TextBlock>
<TextBox Text="{Binding Person[0].FirstName,Mode=TwoWay}" Name="txtFirstName" Margin="115,0,0,106" Background="#FFF0F0F0" />
<TextBlock Grid.Row="1" Text="FirstName"></TextBlock>
<TextBox Grid.Row="1" Text="{Binding Person[0].LastName,Mode=TwoWay}" Name="txtLastName" Margin="115,0,0,110" Background="#FFF0F0F0" />
</Grid>
<Button Content="Update" Command="{Binding UpdateView}" />
</StackPanel>