【问题标题】:Updating View through binding doesnt' work通过绑定更新视图不​​起作用
【发布时间】:2011-12-14 11:47:41
【问题描述】:

我在使用 MVVM 和 Prism 4.0 更新列表框布局时遇到问题。

我没有问题将我的 observablecollection 显示到我的列表框,但是当我将它绑定到 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});
 }
}
}

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>

ProfilePage.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。

【问题讨论】:

  • 你能多贴一点代码吗?您拥有的 Listbox 代码与 VM 或 Person 类不匹配。
  • 很抱歉,我的视图复制粘贴错误。已更新。

标签: .net wpf xaml data-binding prism


【解决方案1】:

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

【讨论】:

  • 如果我将使用 ElementName 绑定,那么我就违反了 MVVM 模式的规则。
  • 不要成为模式的奴隶。 =)
  • 它不是模式的奴隶,它被称为软件架构。不做一个小系统来实施打破规则。
  • @Inluis - 使用 DataContextProxy,这将是 MVVM,无需锚定到另一个 UI 元素。我认为 SL5 将通过绑定改进满足您的需求
【解决方案2】:

如果我没看错,您还需要在 VM 上实现 INotifyPropertyChanged。视图无法看到模型触发的事件。

【讨论】:

  • 它不起作用。我解雇了 INotifyPropertyChanged 和 OnPropertyChanged 并传递了 ObservableCollection Persons 属性。没有任何改变。
猜你喜欢
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 2016-09-16
  • 2018-06-21
  • 2014-10-20
  • 1970-01-01
相关资源
最近更新 更多