2011-10-24 4 views
0

MVVM with 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> 

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(); 
} 
,

Person 클래스는 INotifyPropertyChanged를 구현하며 LastName, FirstName 및 LastUpdated의 getter 설정자가 있습니다.

+0

코드를 더 게시 할 수 있습니까? 보유한 Listbox 코드가 VM 또는 Person 클래스와 일치하지 않습니다. – JMcCarty

+0

죄송합니다. 본인의 견해에 잘못된 사본이 붙여졌습니다. 업데이트 됨. –

답변

0

바인딩을 작동 시키려면 DataContextProxy이 필요합니다. ElementName 바인딩은 ListBox에서도 작동합니다.

+0

ElementName 바인딩을 사용하면 MVVM 패턴 규칙이 위반됩니다. –

+0

패턴을 그리는 노예가되지 마십시오. =) – Yatrix

+0

패턴에 대한 노예가 아니라 소프트웨어 아키텍처라고합니다. 규칙을 깨기 위해 구현할 작은 시스템을 사용하지 마십시오. –

0

제대로 표시하려면 VM에서도 INotifyPropertyChanged를 구현해야합니다. 뷰는 모델에서 시작된 이벤트를 볼 수 없습니다.

+0

작동하지 않습니다. 나는 INotifyPropertyChanged와 OnPropertyChanged를 해고 ObservableCollection Persons 프로퍼티를 전달했습니다. 아무것도 변경되지 않았습니다. –

관련 문제