Hi All,
I’m trying to build the custom grid with fixed Header and Column in Silverlight. I don’t know whether SL has built in functionality to build this or not because I’m very new to SL. Please take a look the following codes. I don’t think that this is the right way to build this. If you don’t mind, please refactor my codes and please advise me the right way.
MainPage.xaml
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Canvas x:Name="LayoutRoot" Background="White">
</Canvas>
MainPage.xaml.cs
using System.Net;
using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.Windows.Controls.Primitives;
namespace SilverlightApplication1 { public partial class MainPage : UserControl {
private Grid theGrid = new Grid();
private Canvas gridContainer = new Canvas();
private Canvas colHeader = new Canvas();
private StackPanel colHeaderContainer = new StackPanel();
private Canvas rowHeader = new Canvas();
private StackPanel rowHeaderContainer = new StackPanel();
private Canvas scrollableGrid = new Canvas();
private Border firstRowfirstColCell = new Border();
private ScrollBar hScrollBar = new ScrollBar();
private ScrollBar vScrollBar = new ScrollBar();
public MainPage()
{
InitializeComponent();
BuildTheGrid();
}
private void BuildTheGrid()
{
for (int i = 0; i <= 10; i++)
{
ColumnDefinition cdef = new ColumnDefinition();
cdef.Width = new GridLength(100);
theGrid.ColumnDefinitions.Add(cdef);
}
for (int i = 0; i <= 8; i++)
{
RowDefinition rdef = new RowDefinition();
rdef.Height = new GridLength(50);
theGrid.RowDefinitions.Add(rdef);
}
for (int i = 0; i <= 10; i++)
{
for (int j = 0; j <= 8; j++)
{
Border mbrd = new Border();
mbrd.BorderBrush = new SolidColorBrush(Colors.Black);
mbrd.BorderThickness = new Thickness(1);
mbrd.Width = 100;
mbrd.Height = 50;
mbrd.SetValue(Grid.RowProperty, j);
mbrd.SetValue(Grid.ColumnProperty, i);
StackPanel sp = new StackPanel();
sp.AllowDrop = true;
sp.Background = new SolidColorBrush(Colors.LightGray);
sp.SetValue(Grid.RowProperty, j);
sp.SetValue(Grid.ColumnProperty, i);
theGrid.Children.Add(sp);
theGrid.Children.Add(mbrd);
}
}
gridContainer.Children.Add(theGrid);
Canvas.SetLeft(scrollableGrid, 350);
Canvas.SetTop(scrollableGrid, 150);
scrollableGrid.Children.Add(gridContainer);
scrollableGrid.Width = 100;
RectangleGeometry recClip = new RectangleGeometry();
recClip.Rect = new Rect(0, 0, 880, 350);
scrollableGrid.Clip = recClip;
this.LayoutRoot.Children.Add(scrollableGrid);
hScrollBar.Orientation = Orientation.Horizontal;
Canvas.SetLeft(hScrollBar, 250);
Canvas.SetTop(hScrollBar, 500);
hScrollBar.Width = 980;
hScrollBar.ViewportSize = 100;
hScrollBar.Maximum = 220;
hScrollBar.Minimum = 0;
hScrollBar.SmallChange = 10;
hScrollBar.LargeChange = 15;
hScrollBar.Value = 0;
hScrollBar.Scroll += hScrollBar_Scroll;
this.LayoutRoot.Children.Add(hScrollBar);
vScrollBar.Orientation = Orientation.Vertical;
Canvas.SetLeft(vScrollBar, 1230);
Canvas.SetTop(vScrollBar, 100);
vScrollBar.Height = 400;
vScrollBar.ViewportSize = 100;
vScrollBar.Maximum = 100;
vScrollBar.Minimum = 0;
vScrollBar.SmallChange = 5;
vScrollBar.LargeChange = 10;
vScrollBar.Value = 0;
vScrollBar.Scroll += vScrollBar_Scroll;
this.LayoutRoot.Children.Add(vScrollBar);
colHeaderContainer.Orientation = Orientation.Horizontal;
for (int i = 0; i <= 10; i++)
{
Border mbrd = new Border();
mbrd.BorderBrush = new SolidColorBrush(Colors.Black);
mbrd.BorderThickness = new Thickness(1);
mbrd.Width = 100;
mbrd.Height = 50;
colHeaderContainer.Children.Add(mbrd);
}
colHeader.Children.Add(colHeaderContainer);
Canvas.SetLeft(colHeader, 350);
Canvas.SetTop(colHeader, 100);
RectangleGeometry colHeaderrecClip = new RectangleGeometry();
colHeaderrecClip.Rect = new Rect(0, 0, 880, 50);
colHeader.Clip = colHeaderrecClip;
this.LayoutRoot.Children.Add(colHeader);
rowHeaderContainer.Orientation = Orientation.Vertical;
for (int i = 0; i <= 8; i++)
{
Border mbrd = new Border();
mbrd.BorderBrush = new SolidColorBrush(Colors.Black);
mbrd.BorderThickness = new Thickness(1);
mbrd.Width = 100;
mbrd.Height = 50;
rowHeaderContainer.Children.Add(mbrd);
}
rowHeader.Children.Add(rowHeaderContainer);
Canvas.SetLeft(rowHeader, 250);
Canvas.SetTop(rowHeader, 150);
RectangleGeometry rowHeaderrecClip = new RectangleGeometry();
rowHeaderrecClip.Rect = new Rect(0, 0, 100, 350);
rowHeader.Clip = rowHeaderrecClip;
this.LayoutRoot.Children.Add(rowHeader);
this.LayoutRoot.Children.Add(firstRowfirstColCell);
firstRowfirstColCell.BorderBrush = new SolidColorBrush(Colors.Black);
firstRowfirstColCell.BorderThickness = new Thickness(1);
firstRowfirstColCell.Width = 100;
firstRowfirstColCell.Height = 50;
Canvas.SetLeft(firstRowfirstColCell, 250);
Canvas.SetTop(firstRowfirstColCell, 100);
theGrid.MouseWheel += theGrid_MouseWheel;
}
private void hScrollBar_Scroll(object sender, ScrollEventArgs e)
{
gridContainer.SetValue(Canvas.LeftProperty, -hScrollBar.Value);
colHeaderContainer.SetValue(Canvas.LeftProperty, -hScrollBar.Value);
}
private void vScrollBar_Scroll(object sender, ScrollEventArgs e)
{
gridContainer.SetValue(Canvas.TopProperty, -vScrollBar.Value);
rowHeaderContainer.SetValue(Canvas.TopProperty, -vScrollBar.Value);
}
private void theGrid_MouseWheel(object sender, MouseWheelEventArgs e)
{
vScrollBar.Value += e.Delta > 0 ? -vScrollBar.LargeChange : vScrollBar.LargeChange;
gridContainer.SetValue(Canvas.TopProperty, -vScrollBar.Value);
rowHeaderContainer.SetValue(Canvas.TopProperty, -vScrollBar.Value);
}
}
}
Thanks,
aghein.ng