2016-12-07 5 views
0

したがって、ObservableCollectionにバインドされたUWPアプリケーションでGridViewを作成しています。UWP Gridview ObservableCollectionにバインドして値をクリアする

XAML:

<Page.Resources> 
    <CollectionViewSource 
      x:Name="ListSource" 
      Source="{x:Bind model.ShowList}" 
      IsSourceGrouped="false" 
      /> 
</Page.Resources> 
... 
<Page> 
    <GridView x:Name="lvListSource" ItemsSource="{Binding Source={StaticResource ListSource}}" SelectedIndex="-1" Grid.Row="2" HorizontalContentAlignment="Center" SelectionChanged="lvEpisodeListSource_SelectionChanged"> 
       <GridView.ItemTemplate> 
        <DataTemplate x:DataType="local:PageInput"> 
         <TextBlock Name="AlbumBlock" Foreground="Black" FontWeight="Normal" FontSize="15" Margin="5,0,5,0" 
            Text="{x:Bind Name}" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100"/> 
        </DataTemplate> 
       </GridView.ItemTemplate> 
      </GridView> 
</page> 

C#の:私はコレクションに項目を追加するとき

public MainCategoryModel model; 
public MainPage() 
{ 
    model = new MainCategoryModel(); 
    this.InitializeComponent(); 
} 

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    //initialize model data in code and stuff comes out correctly 
    //model.Clear() give unknown error here 
    model.Add(new PageInput() {...}); 
    lvListSource.ItemsSource = model.myList; 
} 

public class MainCategoryModel 
{ 
    public ObservableCollection<PageInput> myList { get; set; } 

    public MainCategoryModel() 
    { 
     myList = new ObservableCollection<PageInput>(); 
    } 

    public void AddShow(PageInput show) 
    { 
     myList.Insert(0, show); 
    } 

    public void Clear() 
    { 
     myList.Clear(); 
    } 
} 

のObservableCollectionは完全に罰金適切に初期化し、ページがロードされます。問題は、このObservableCollectionを更新して、OnNavigatedTo関数が呼び出されるたびにGridViewがバインドされていることです。これには、コレクションからアイテムを削除することも含まれます。アイテムを削除するかObservableCollectionをクリアしようとするたびに、ページは不明なエラーで読み込まれません。既にバインドされているObservable Collectionから値を変更して削除する方法はありますか?

OnNavigatedToの最後にItemsSourceプロパティを再設定しないと、XAMLで設定されているため、コレクションに値を追加した直後にSelectionChangedイベントが呼び出されます。アイテムがGridViewに追加されたときにSelectionChangedイベントが呼び出されないようにする方法はありますか?

答えて

0

X:バインドはデフォルトでワンタイムれる、あなたは、

protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     //initialize model data in code and stuff comes out correctly 

     lvEpisodeListSource.ItemsSource = model.myList; 
    } 

はあなたが結合殺すレイアウトの更新を取得するモード=一方向に設定model.myListを更新し、バインディングが仕事をするようにする必要があり、ドン」あなたはそれがバインドされたい場合はtが、コードからこの方法でのItemsSourceを設定

編集:

をコピーして(あなたがそれをきれいにしなかったので、楽しい経験ではなかった)あなたのコードを実行し、私はしないでくださいあなたのコードにBindingとx:Bindを混ぜるのはなぜですか?そのようなコレクションをバインドするだけですか?

<GridView x:Name="lvListSource" ItemsSource="{x:Bind model.myList, Mode=OneWay}" .... 

EDIT2:

マイコード:

0123:quess製

public class MainCategoryModel 
{ 
    public ObservableCollection<PageInput> myList { get; set; } 

    public MainCategoryModel() 
    { 
     myList = new ObservableCollection<PageInput>(); 
    } 

    public void AddShow(PageInput show) 
    { 
     myList.Insert(0, show); 
    } 

    public void Clear() 
    { 
     myList.Clear(); 
    } 
} 

<Page> 
(........) 
    <GridView x:Name="lvListSource" ItemsSource="{x:Bind model.myList, Mode=OneWay}" SelectedIndex="-1" Grid.Row="2" HorizontalContentAlignment="Center"> 
    <GridView.ItemTemplate> 
     <DataTemplate x:DataType="local:PageInput"> 
      <TextBlock Name="AlbumBlock" Foreground="Black" FontWeight="Normal" FontSize="15" Margin="5,0,5,0" 
           Text="{x:Bind Name}" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100"/> 
     </DataTemplate> 
    </GridView.ItemTemplate> 
</GridView> 

public sealed partial class MainPage : Page 
{ 
    public MainCategoryModel model; 
    public MainPage() 
    { 
     model = new MainCategoryModel(); 
     this.InitializeComponent(); 
    } 

    protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     model.Clear(); 
     model.myList.Add(new PageInput() {Name = "testName"}); 
    } 

}

public class PageInput 
{ 
    public string Name { get; set; } 
} 

は、XAMLコードは、ページ・キャッシュをオンにすると不明なエラーを与えるためにバインドされているデータオブジェクトのオブジェクトをクリア

+0

私はこれを削除しても、コレクションからアイテムを削除しようとするとクラッシュするという同じ問題が発生します。また、アイテムがない場合はアイテムを追加すると、自動的にSelectionChangedイベントが呼び出されます。 –

+0

はあなたのコードを取り出して実行可能にしました。 – RTDev

+0

こんにちは、そのバインディングは完璧に機能しました!あなたはそれをそうすることができるとは気づいていませんでした。最初の実行ではすべて完全に動作しますが、各アイテムにはSelectionChangedイベントがあり、別のページを呼び出します。あなたのために編集で追加されました。組み込みのApp Backボタンを使用すると、Clear()関数を実行したときに未処理のXAMLエラーが発生します。 –

0

を実行し、それをチェックしてみてください。ページキャッシュを無効にすると、オブジェクトを正しくクリアすることができます。

this.NavigationCacheMode = NavigationCacheMode.Disabled; 
関連する問題