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
Imports C1.Silverlight.FlexGrid

Namespace TraditionalIndexing
	Partial Public Class MainPage
		Inherits UserControl
		Public Sub New()
			InitializeComponent()

			' create some data
			Dim list_Renamed = New List(Of Customer)()
			For i As Integer = 0 To 199
				list_Renamed.Add(New Customer(i))
			Next i

			' create a flexgrid with traditional indexing
			Dim flex = New FlexGridTraditionalIndexing()
			LayoutRoot.Children.Add(flex)

			' set number of rows and columns
			flex.Rows.Fixed = 3
			flex.Columns.Fixed = 3

			' bind it to our data
			flex.ItemsSource = list_Renamed

			' get two values using traditional indexing 
			' (first scrollable row, first scrollable column)
			Debug.WriteLine(flex(flex.Rows.Fixed, flex.Columns.Fixed))
			Debug.WriteLine(flex(flex.Rows.Fixed, flex.Columns.Fixed + 1))
		End Sub
	End Class

	Friend Class FlexGridTraditionalIndexing
		Inherits C1FlexGrid
		' ** fields
		Private _rows As RowCollectionTraditionalIndexing
		Private _cols As ColumnCollectionTraditionalIndexing

		' ** ctor
		Public Sub New()
			' create custom row and column collections to provide
			' a 'Fixed' property and the traditional FlexGrid indexing methods
			_rows = New RowCollectionTraditionalIndexing(Me)
			_cols = New ColumnCollectionTraditionalIndexing(Me)
		End Sub
		Public Shadows ReadOnly Property Rows() As RowCollectionTraditionalIndexing
			' return custom row collection
			Get
				Return _rows
			End Get
		End Property
		Public Shadows ReadOnly Property Columns() As ColumnCollectionTraditionalIndexing
			' return custom column collection
			Get
				Return _cols
			End Get
		End Property
		Default Public Shadows Property Item(ByVal row As Integer, ByVal col As Integer) As Object
			' override direct data access
			Get
				Return Me.Rows(row)(Me.Columns(col))
			End Get
			Set(ByVal value As Object)
				Me.Rows(row)(Me.Columns(col)) = value
			End Set
		End Property

		' Row collection that uses the traditional FlexGrid indexing method:
		' The "Fixed" property gets or sets the number of fixed rows.
		' Row indices from 0 to "Fixed" correspond to column header rows,
		' Row indices from "Fixed" on correspond to scrollable rows 0, 1, etc...
		Public Class RowCollectionTraditionalIndexing
			' ** fields
			Private _grid As FlexGridTraditionalIndexing

			' ** ctor
			Public Sub New(ByVal grid As FlexGridTraditionalIndexing)
				_grid = grid
			End Sub

			' ** object model
			Public Property Fixed() As Integer
				Get
					Return _grid.ColumnHeaders.Rows.Count
				End Get
				Set(ByVal value As Integer)
					Do While Fixed < value
						_grid.ColumnHeaders.Rows.Add(New Row())
					Loop
					Do While Fixed > value
						_grid.ColumnHeaders.Rows.RemoveAt(_grid.ColumnHeaders.Rows.Count - 1)
					Loop
				End Set
			End Property
			Default Public ReadOnly Property Item(ByVal index As Integer) As Row
				Get
					Return If(index < Fixed, _grid.ColumnHeaders.Rows(index), _grid.Cells.Rows(index - Fixed))
				End Get
			End Property
		End Class

		' Column collection that uses the traditional FlexGrid indexing method:
		' The "Fixed" property gets or sets the number of fixed columns.
		' Column indices from 0 to "Fixed" correspond to row header columns,
		' Column indices from "Fixed" on correspond to scrollable columns 0, 1, etc...
		Public Class ColumnCollectionTraditionalIndexing
			' ** fields 
			Private _grid As FlexGridTraditionalIndexing

			' ** ctor
			Public Sub New(ByVal grid As FlexGridTraditionalIndexing)
				_grid = grid
			End Sub

			' ** object model
			Public Property Fixed() As Integer
				Get
					Return _grid.RowHeaders.Columns.Count
				End Get
				Set(ByVal value As Integer)
					Do While Fixed < value
						_grid.RowHeaders.Columns.Add(New Column())
					Loop
					Do While Fixed > value
						_grid.RowHeaders.Columns.RemoveAt(_grid.RowHeaders.Columns.Count - 1)
					Loop
				End Set
			End Property
			Default Public ReadOnly Property Item(ByVal index As Integer) As Column
				Get
					Return If(index < Fixed, _grid.RowHeaders.Columns(index), _grid.Cells.Columns(index - Fixed))
				End Get
			End Property
		End Class
	End Class
End Namespace
