Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports C1.Silverlight.FlexGrid

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

			' create the C1FlexGrid
			Dim flex = New C1FlexGrid()
			LayoutRoot.Children.Add(flex)

			' change appearance a little
			flex.RowBackground = New SolidColorBrush(Color.FromArgb(&Hff, 255, 215, 0))
			flex.AlternatingRowBackground = flex.RowBackground
			flex.GridLinesVisibility = GridLinesVisibility.All
			flex.Rows.DefaultSize = 60
			flex.Columns.DefaultSize = 100
			flex.RowHeaders.Columns.DefaultSize = 80

			flex.VerticalAlignment = System.Windows.VerticalAlignment.Center
			flex.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
			flex.ColumnHeaders.Rows(0).HeaderHorizontalAlignment = System.Windows.HorizontalAlignment.Center
			flex.RowHeaders.Columns(0).HeaderHorizontalAlignment = System.Windows.HorizontalAlignment.Right

			' activate custom merge manager
			flex.AllowMerging = AllowMerging.All
			flex.MergeManager = New MyMergeManager()

			' add some unbound rows and columns
			For cols As Integer = 0 To 6
				flex.Columns.Add(New Column() With {.HorizontalAlignment = HorizontalAlignment.Center})
			Next cols
			For rows As Integer = 0 To 4
				flex.Rows.Add(New Row() With {.VerticalAlignment = VerticalAlignment.Center})
			Next rows

			' set column headers
			SetData(flex.ColumnHeaders.Rows(0), "Monday" & vbTab & "Tuesday" & vbTab & "Wednesday" & vbTab & "Thursday" & vbTab & "Friday" & vbTab & "Saturday" & vbTab & "Sunday")

			' add data
			SetData(flex, 0, "12:00", "Walker" & vbTab & "Morning Show" & vbTab & "Morning Show" & vbTab & "Sport" & vbTab & "Weather" & vbTab & "N/A" & vbTab & "N/A")
			SetData(flex, 1, "13:00", "Today Show" & vbTab & "Today Show" & vbTab & "Sesame Street" & vbTab & "Football" & vbTab & "Market Watch" & vbTab & "N/A" & vbTab & "N/A")
			SetData(flex, 2, "14:00", "Today Show" & vbTab & "Today Show" & vbTab & "Kid Zone" & vbTab & "Football" & vbTab & "Soap Opera" & vbTab & "N/A" & vbTab & "N/A")
			SetData(flex, 3, "15:00", "News" & vbTab & "News" & vbTab & "News" & vbTab & "News" & vbTab & "News" & vbTab & "N/A" & vbTab & "N/A")
			SetData(flex, 4, "16:00", "News" & vbTab & "News" & vbTab & "News" & vbTab & "News" & vbTab & "News" & vbTab & "N/A" & vbTab & "N/A")

		End Sub
		Private Sub SetData(ByVal row_Renamed As Row, ByVal content As String)
			Dim items = content.Split(ControlChars.Tab)
			For i As Integer = 0 To items.Length - 1
				row_Renamed(i) = items(i)
			Next i
		End Sub
		Private Sub SetData(ByVal flex As C1FlexGrid, ByVal row As Integer, ByVal rowHeader As String, ByVal content As String)
			flex.RowHeaders(row, 0) = rowHeader
			SetData(flex.Rows(row), content)
		End Sub

		''' <summary>
		''' Custom merge manager that creates cell ranges spanning multiple rows and columns
		''' </summary>
		Public Class MyMergeManager
			Implements IMergeManager
            Public Function GetMergedRange(ByVal grid As C1FlexGrid, ByVal cellType_Renamed As CellType, ByVal rg As CellRange) As CellRange Implements IMergeManager.GetMergedRange
                ' we are only interested in data cells 
                ' (not merging row or column headers)
                If cellType_Renamed = CellType.Cell Then
                    ' expand left/right
                    For i As Integer = rg.Column To grid.Columns.Count - 2
                        If GetDataDisplay(grid, rg.Row, i) <> GetDataDisplay(grid, rg.Row, i + 1) Then
                            Exit For
                        End If
                        rg.Column2 = i + 1
                    Next i
                    For i As Integer = rg.Column To 1 Step -1
                        If GetDataDisplay(grid, rg.Row, i) <> GetDataDisplay(grid, rg.Row, i - 1) Then
                            Exit For
                        End If
                        rg.Column = i - 1
                    Next i

                    ' expand up/down
                    For i As Integer = rg.Row To grid.Rows.Count - 2
                        If GetDataDisplay(grid, i, rg.Column) <> GetDataDisplay(grid, i + 1, rg.Column) Then
                            Exit For
                        End If
                        rg.Row2 = i + 1
                    Next i
                    For i As Integer = rg.Row To 1 Step -1
                        If GetDataDisplay(grid, i, rg.Column) <> GetDataDisplay(grid, i - 1, rg.Column) Then
                            Exit For
                        End If
                        rg.Row = i - 1
                    Next i
                End If

                ' done
                Return rg
            End Function
			Private Function GetDataDisplay(ByVal grid As C1FlexGrid, ByVal r As Integer, ByVal c As Integer) As String
				Return grid(r, c).ToString()
			End Function
		End Class
	End Class
End Namespace
