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));
}
}
}
}
嗨胡安帕布羅,這很有趣我的名字也是胡安帕布羅。我見過你的代碼,我的工作是通過你說的,我實例化了我的ItemSource,並更新了新的集合,並將其重新分配給我的視圖源。 –
ItemSource = new ObservableCollection>((AlphaKeyGroup