2015-07-21 94 views
0

我尝试了一切,但无法更新可观察集合并在UI中反映出来。我有一个方法将一个新的条目添加到绑定到绑定到我的集合的CollectionViewSource的集合中。当我运行应用程序通用应用程序时,我正确地获取列表,但如果我在点击时添加一个值,则没有任何内容会被反映。有什么建议么?Observable Collection添加项目时未更新

的XAML看起来是这样的:

<SemanticZoom> 
       <SemanticZoom.ZoomedInView> 
        <ListView IsHoldingEnabled="True" 
          ItemsSource="{Binding Mode=OneWay, Source={StaticResource MenuGroups}}" 
          ItemTemplate="{StaticResource MenuItemTemplate}" 
          ContinuumNavigationTransitionInfo.ExitElementContainer="True"> 
         <ListView.GroupStyle> 
          <GroupStyle HidesIfEmpty="True" HeaderTemplate="{StaticResource MenuGroupHeaderTemplate}"/> 
         </ListView.GroupStyle> 
        </ListView> 
       </SemanticZoom.ZoomedInView> 
       <SemanticZoom.ZoomedOutView> 
        <GridView Background="Black" 
          ItemsSource="{Binding Source={StaticResource MenuGroups}, Path=CollectionGroups}" 
          ItemTemplate="{StaticResource MenuJumpTemplate}"> 
        </GridView> 
       </SemanticZoom.ZoomedOutView> 
      </SemanticZoom> 

这里是我后面的代码代码:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 
using TestWin.TestWinService; 
using System.Globalization; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 

namespace TestWin 
{ 
    /// <summary> 
    /// An empty page that can be used on its own or navigated to within a Frame. 
    /// </summary> 
    public sealed partial class MenuPage : Page 
    { 
     #region Members 
     TestWinConnectionClient TestWinService; 
     ObservableCollection<Menu> ocMenuItems = null; 
     ObservableCollection<AlphaKeyGroup<Menu>> ocItemSource = null; 
     #endregion Members 

     #region Properties 
     public ObservableCollection<Menu> MenuItems 
     { 
      get 
      { 
       if (ocMenuItems == null) 
       { 
        ocMenuItems = new ObservableCollection<Menu>(); 

       } 
       return ocMenuItems; 
      } 
      set 
      { 
       ocMenuItems = value;   
       OnPropertyChanged("MenuItems"); 
      } 
     } 

     public ObservableCollection<AlphaKeyGroup<Menu>> ItemSource 
     { 
      get 
      { 
       if (ocItemSource == null) 
       { 
        ocItemSource = new ObservableCollection<AlphaKeyGroup<Menu>>((AlphaKeyGroup<Menu>.CreateGroups(MenuItems, CultureInfo.CurrentUICulture, s => s.MenuName, true))); 
       } 
       return ocItemSource; 
      } 
      set 
      { 
       ocItemSource = value; 
       OnPropertyChanged("ItemSource"); 
      } 
     } 
     #endregion Properties 

     public MenuPage() 
     { 
      TestWinService = new TestWinConnectionClient(); 
      this.InitializeComponent();    
      #region Events 
      this.Loaded += MenuPage_Loaded; 
      #endregion 
     } 

     private void MenuPage_Loaded(object sender, RoutedEventArgs e) 
     { 
      SetItemSource(); 
     } 

     private async void SetItemSource() 
     { 
      MenuItems = await TestWinService.GetMenuEntriesAsync();   
      ((CollectionViewSource)Resources["MenuGroups"]).Source = ItemSource; 
     } 

     private void TextBlock_Tapped(object sender, TappedRoutedEventArgs e) 
     { 
      Menu m = new Menu(); 
      m.MenuName = "Test Entry"; 
      m.SysRowID = Guid.NewGuid(); 
      MenuItems.Add(m); 
      //this.Frame.Navigate(typeof(MenuPage)); 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     void OnPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 

回答

0

如果添加了新的项目,你也需要刷新的来源的MenuItems CollectionViewSource。

可能是你有一个ObservableCollection(的ItemSource)不是neccesary,你可以看看我是如何做到了在Codeproject

,并添加例如:

private async void Test() 
    { 
     await Task.Delay(5000); 
     for (int i = 0; i < 5; i++) 
     { 
      Favorites.Add(new Favorite() 
      { 
       Name = $"AAFriends {i}", 
       Category = new Category() { Name = "Friends" }, 
       Uri = "http://www.expediteapps.com/blog/" 
      }); 
     } 
     InitializeGrouping(); 
    } 

,只有当刷新我再次调用分组。

+0

嗨胡安帕布罗,这很有趣我的名字也是胡安帕布罗。我见过你的代码,我的工作是通过你说的,我实例化了我的ItemSource,并更新了新的集合,并将其重新分配给我的视图源。 –

+0

ItemSource = new ObservableCollection >((AlphaKeyGroup

.CreateGroups(MenuItems,CultureInfo.CurrentUICulture,s => s.MenuName,true))); ((CollectionViewSource)Resources [“MenuGroups”])。Source = ItemSource; 我将不得不了解可观察的集合更多我还没有看到可观察的部分。 谢谢! –

相关问题