通过绑定更新视图不会”工作
我在使用 MVVM 和 Prism 4.0 更新列表框的布局时遇到问题。
我在将可观察集合显示到列表框时没有问题,但是当我将其绑定到 DelegateCommand 以添加新用户或更新选定的列表框项目时,它不会更新,但底层对象正在更新。我尝试使用 MessageBox.Show 为我提供最近的输出,它进行了更改,但在 view.xaml 中它没有更新。
public class ProfileViewModel : DependencyObject
{
public DelegateCommand SaveCommand { get; set; }
public ObservableCollection<Persons> Persons { get; set; }
public ProfileViewModel()
{
CreatePerson();
SaveCommand = new DelegateCommand(Save,CanSave);
}
private void Save()
{
Person[0].LastUpdated = DateTime.Now
Persons.Add(new Persons { FIrstName = "Bob", LastName "Bob," LastUpdated=DateTime.Now});
}
private bool CanSave()
{
return true;
}
public void CreatePerson()
{
this.Persons = new ObservableCollection<Persons>();
Persons.Add(new Persons { FirstName = "John", LastName = "Doe", LastUpdated = DateTime.Now});Persons.Add(new Persons { FirstName = "John", LastName = "Doe", LastUpdated = DateTime.Now});
Persons.Add(new Persons { FirstName = "John", LastName = "Doe", LastUpdated = DateTime.Now});
}
}
}
个人资料页.Xaml
<ListBox ItemsSource="{Binding Persons}" Name="ListBoxItem">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}" />
<Button Content="_Save" Command={Binding Source={Static Resource ProfileViewModel{, Path=SaveCommand}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
ProflilePage.xaml.cs
public partial class ProfilePage : Window
{
private ProfileViewModel _vm;
[Dependency]
public ProfileViewModel VM
{
set { _vm = value; this.DataContext = _vm; }
}
public ProfilePage()
{
InitializeComponent();
}
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
IUnityContainer container = new UnityContainer();
ProfileViewModel source = new ProfileViewModel();
ProfilePage window = container.Resolve<ProfilePage>();
window.show();
}
我的 Persons 类实现 INotifyPropertyChanged 并具有 LastName、FirstName 和 LastUpdated 的 getter setter。
I have a problem updating the layout of my listbox using MVVM with Prism 4.0.
I have no problem displaying my observablecollection to my listbox, but when I bind it to the DelegateCommand to add a new user or update a selected listbox item, it doesn't update but the underlying object is being updated. I tried using MessageBox.Show to give me the recent output and it did the changes, but in the view.xaml it doesn't update.
public class ProfileViewModel : DependencyObject
{
public DelegateCommand SaveCommand { get; set; }
public ObservableCollection<Persons> Persons { get; set; }
public ProfileViewModel()
{
CreatePerson();
SaveCommand = new DelegateCommand(Save,CanSave);
}
private void Save()
{
Person[0].LastUpdated = DateTime.Now
Persons.Add(new Persons { FIrstName = "Bob", LastName "Bob," LastUpdated=DateTime.Now});
}
private bool CanSave()
{
return true;
}
public void CreatePerson()
{
this.Persons = new ObservableCollection<Persons>();
Persons.Add(new Persons { FirstName = "John", LastName = "Doe", LastUpdated = DateTime.Now});Persons.Add(new Persons { FirstName = "John", LastName = "Doe", LastUpdated = DateTime.Now});
Persons.Add(new Persons { FirstName = "John", LastName = "Doe", LastUpdated = DateTime.Now});
}
}
}
ProfilePage.Xaml
<ListBox ItemsSource="{Binding Persons}" Name="ListBoxItem">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}" />
<Button Content="_Save" Command={Binding Source={Static Resource ProfileViewModel{, Path=SaveCommand}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
ProflilePage.xaml.cs
public partial class ProfilePage : Window
{
private ProfileViewModel _vm;
[Dependency]
public ProfileViewModel VM
{
set { _vm = value; this.DataContext = _vm; }
}
public ProfilePage()
{
InitializeComponent();
}
App.xaml.cs
protected override void OnStartup(StartupEventArgs e)
{
IUnityContainer container = new UnityContainer();
ProfileViewModel source = new ProfileViewModel();
ProfilePage window = container.Resolve<ProfilePage>();
window.show();
}
My Persons class implements the INotifyPropertyChanged and has a getter setter of LastName,FirstName and LastUpdated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很确定您需要 DataContextProxy 使绑定正常工作。 ElementName 绑定也适用于 ListBox
I am pretty sure you need the DataContextProxy to make bindings working. The ElementName binding will also work on ListBox
如果我没有看错的话,您还需要在虚拟机上实现 INotifyPropertyChanged。视图无法看到从模型激发的事件。
If I am looking at this correctly, you need to Implement INotifyPropertyChanged on the VM as well. The View cannot see the events fired from the Model.