通过绑定更新视图不​​会”工作

发布于 2024-12-12 05:54:29 字数 2502 浏览 0 评论 0原文

我在使用 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长伴 2024-12-19 05:54:29

我很确定您需要 DataContextProxy 使绑定正常工作。 ElementName 绑定也适用于 ListBox

I am pretty sure you need the DataContextProxy to make bindings working. The ElementName binding will also work on ListBox

茶色山野 2024-12-19 05:54:29

如果我没有看错的话,您还需要在虚拟机上实现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文