Imports System.Net
Imports System.Windows.Media.Animation
Imports C1.WPF.FlexGrid

Namespace CustomTypeDescriptor
	Public Class FinancialCellFactory
		Inherits CellFactory
		Private Shared _thicknessEmpty As New Thickness(0)

		' bind cell to ticker
		Public Overrides Sub CreateCellContent(ByVal grid As C1FlexGrid, ByVal bdr As Border, ByVal range As CellRange)
			' create binding for this cell
			Dim r = grid.Rows(range.Row)
			Dim c = grid.Columns(range.Column)
			Dim pi = c.PropertyInfo
			If TypeOf r.DataItem Is FinancialData AndAlso (pi.Name = "LastSale" OrElse pi.Name = "Bid" OrElse pi.Name = "Ask") Then
				Dim ticker As New StockTicker()
				bdr.Child = ticker
				bdr.Padding = _thicknessEmpty
				If True Then
					' to show sparklines
					ticker.Tag = r.DataItem
					ticker.BindingSource = pi.Name

					' traditional binding
					Dim binding_Renamed = New Binding(pi.Name)
					binding_Renamed.Source = r.DataItem
					binding_Renamed.Mode = BindingMode.OneWay
					ticker.SetBinding(StockTicker.ValueProperty, binding_Renamed)
				Else
					' OK to use INotifyPropertyChanged for one-way bindings
					ticker.Value = CDbl(CDec(pi.GetValue(r.DataItem, Nothing)))
					AddHandler (CType(r.DataItem, FinancialData)).PropertyChanged, Function(sender, e) AnonymousMethod1(sender, e, pi, ticker)
				End If
			Else
				' use default implementation
				MyBase.CreateCellContent(grid, bdr, range)
			End If
		End Sub
		
        Private Function AnonymousMethod1(ByVal sender As Object, ByVal e As Object, ByVal pi As Reflection.PropertyInfo, ByVal ticker As StockTicker) As Object
            If e.PropertyName = pi.Name Then
                ticker.Value = CDbl(CDec(pi.GetValue(sender, Nothing)))
            End If
            Return Nothing
        End Function
	End Class
End Namespace
