Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes

Namespace CustomColumns
	Partial Public Class MainPage
		Inherits UserControl
		' data source
		Private _view As System.ComponentModel.ICollectionView = Product.GetProducts(200)

		Public Sub New()
			InitializeComponent()

			' bind the grid to the data
			_flex.ItemsSource = _view
		End Sub

		' handle events
		Private Sub ShowDetail_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
			Dim product_Renamed = GetProduct(sender)
			If product_Renamed IsNot Nothing Then
				Dim detail = String.Format("Product {0}, Color {1}, Rating {2}", product_Renamed.Name, product_Renamed.Color, product_Renamed.Rating)
				MessageBox.Show(detail, "Detail", MessageBoxButton.OK)
			End If
		End Sub
		Private Sub DeleteButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
			Dim product_Renamed = GetProduct(sender)
			If product_Renamed IsNot Nothing Then
				Dim result = MessageBox.Show("Are you sure you want to delete this item?", "Confirm", MessageBoxButton.OKCancel)
                If result = MessageBoxResult.OK Then
                    Dim ev = TryCast(_view, System.ComponentModel.IEditableCollectionView)
                    ev.Remove(product_Renamed)
                End If
			End If
		End Sub
		Private Sub MoveUp_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
			Dim product_Renamed = GetProduct(sender)
			Dim list = TryCast(_view.SourceCollection, IList(Of Product))
			Dim index = list.IndexOf(product_Renamed)
			If index > 0 Then
				list.RemoveAt(index)
				list.Insert(index - 1, product_Renamed)
				_flex.Invalidate()
			End If
		End Sub
		Private Sub MoveDown_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
			Dim product_Renamed = GetProduct(sender)
			Dim list = TryCast(_view.SourceCollection, IList(Of Product))
			Dim index = list.IndexOf(product_Renamed)
			If index < list.Count - 1 Then
				list.RemoveAt(index)
				list.Insert(index + 1, product_Renamed)
				_flex.Invalidate()
			End If
		End Sub

		' get the product that is represented by a control on the grid
		Private Function GetProduct(ByVal control As Object) As Product
			Dim e As FrameworkElement = TryCast(control, FrameworkElement)
			Return TryCast(e.DataContext, Product)
		End Function
	End Class
End Namespace
