我有一个模特
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;
}
}
Usercontrol是
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<local:PersonViewModel></local:PersonViewModel>
</UserControl.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="FirstName"></TextBlock>
<TextBox Text="{Binding FirstName,Mode=TwoWay}" Name="txtFirstName" Margin="115,0,0,106" DataContext="{Binding Path=Person}" Background="#FFF0F0F0" />
<TextBlock Grid.Row="1" Text="FirstName"></TextBlock>
<TextBox Grid.Row="1" Text="{Binding LastName,Mode=TwoWay}" Name="txtLastName" Margin="115,0,0,110" DataContext="{Binding Path=Person}" Background="#FFF0F0F0" />
</Grid>
</UserControl>
我需要重新绑定未发生的datacontext
can you suggest what is the problem?
答案 0 :(得分:0)
ViewModel未实现INotifyPropertyChanged,因此在设置集合时,视图不会通知此更改。
一般来说,我建议不要使用此设置:您正在混合模型和ViewModel,这将在模型的视图中创建依赖关系。虽然它现在可能在将来的版本中起作用,但它可能会成为一个问题,而且它违背了MVVM模式。
答案 1 :(得分:0)
更改此代码:
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>