私はWindows8アプリ(XAML-C#-Windowsストアアプリ(メトロ))を作成しています。このアプリでは、GridViewのSelectedItemsコレクションをループする必要があります。
これが私のコードです:
private void bottomAppBarBotonEliminar_Tapped(object sender, TappedRoutedEventArgs e)
{
//Borrar el(los) elemento(s) seleccionado(s)
foreach (GridViewItem elItem in GVElementos.SelectedItems)
{
MiColeccion.RemoveAt(GVElementos.Items.IndexOf(elItem));
}
ElementoSQL.Sincronizar(MiColeccion);
}
それを実行してそのメソッドを起動すると、次のエラーが発生します(スペイン語から翻訳):
An exception of type 'System.InvalidCastException' occurred
in Lista.exe but was not handled in user code
Additional information: Unable to convert an object of
type 'System.String' to the type 'Windows.UI.Xaml.Controls.GridViewItem'.
プログラムが中断すると、VisualStudioはforeachステートメントで行を強調表示します。
「GVElementos」はXAMLGridViewです。
タイプ「GridViewItem」および「GVElementos.SelectedItems」の「elItem」は、タイプ「GridViewItem」の要素のコレクションではありませんか?
私は何が間違っているのですか?GridViewを繰り返す別の方法はありますか?私はASP.NETから来ており、この方法が理にかなっています。