2015-09-13 14 views
6

Tôi khá mới đối với C# và cố gắng phát triển ứng dụng Windows 10 UWP làm dự án sở thích.C# XAML: Giao diện người dùng không cập nhật sau khi thay đổi một thể hiện của ObservableCollection

Một phần của MainBrowser.XAML

  <GridView x:Name="browserPane" ItemsSource="{x:Bind fileInCurrentFolderList,Mode=OneWay}" SelectionChanged="browserPane_SelectionChanged" Margin="0,48,0,0"> 
       <GridView.ItemTemplate> 
        <DataTemplate x:DataType="classes:Item"> 
         <StackPanel Margin="10"> 
          <Image Height="268" Width="200" Source="{x:Bind Thumbnail}" x:Phase="1" Stretch="UniformToFill"></Image> 
          <TextBlock Width="200" Text="{x:Bind Caption}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" MaxLines="2"/> 
         </StackPanel> 
        </DataTemplate> 
       </GridView.ItemTemplate> 
      </GridView> 

xem lưới Đây là ràng buộc để

public sealed partial class MainBrowser : Page 
{ 
... 
private ObservableCollection<Item> fileInCurrentFolderListUI = new ObservableCollection<Item>(); 
... 
} 

Có một danh sách nút ở phía bên trái của ứng dụng. Mỗi nút sẽ gọi một phương thức sẽ trả về ObservableCollection<Item>.

Vấn đề là tôi cần phải làm một cái gì đó giống như

foreach (Item file in ReturnObservableCollection) 
    { 
    fileInCurrentFolderList.Add(item); 
    } 

Thay vì điều này

fileInCurrentFolderList = ReturnObservableCollection; 

Để có thể kích hoạt cập nhật trong giao diện người dùng. Làm cách nào để thay đổi điều này?

Trả lời

7

Điều đang xảy ra là ObservableCollection đang báo cáo khi các mục được thêm vào hoặc bị xóa khỏi nó, nhưng nếu bản thân bộ sưu tập thay đổi (tức là một thể hiện mới được khởi tạo) thì không có gì để báo cáo thay đổi. Một giải pháp là sử dụng giao diện INotifyPropertyChanged trong ViewModel và báo cáo các thay đổi đối với thuộc tính.

public sealed partial class MainBrowser : Page, INotifyPropertyChanged 
{ 
    // Backing field. 
    private ObservableCollection<Item> fileInCurrentFolderListUI; 
    // Property. 
    public ObservableCollection<Item> FileInCurrentFolderListUI 
    { 
     get { return fileInCurrentFolderListUI; } 
     set 
     { 
      if (value != fileInCurrentFolderListUI) 
      { 
       fileInCurrentFolderListUI = value; 
       // Notify of the change. 
       NotifyPropertyChanged(); 
      } 
     } 
    } 

    // PropertyChanged event. 
    public event PropertyChangedEventHandler PropertyChanged; 

    // PropertyChanged event triggering method. 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

Bạn có thể khởi tạo các lĩnh vực sao lưu trong tờ khai như bạn đã làm trước đó hay chỉ là khởi tạo các tài sản trong các nhà xây dựng. Chỉ cần chắc chắn rằng bạn liên kết với thuộc tính và không phải là trường sao lưu. Ngoài ra, nếu bạn định chỉ định một đối tượng mới, hãy đảm bảo bạn thực hiện nó cho thuộc tính để thay đổi có thể được phát. Về cơ bản, không tương tác với lĩnh vực sao lưu, chỉ cần làm tất cả mọi thứ thông qua tài sản.

+2

Cảm ơn bạn! Bây giờ, nó hoạt động như nó phải được. – Suttisak

+1

@Suttisak Bạn được chào đón. –

+1

@ B.K. Sử dụng tuyệt vời của CallerMemberName. Tôi đã luôn luôn thông qua giá trị của tên thành viên gọi điện thoại, và điều này làm cho nó thanh lịch hơn rất nhiều! Cảm ơn! – RAB

Các vấn đề liên quan