开发者

Silverlight Custom Grid with Fixed Header and Column, required refactoring

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 code. I don’t think that this is the right way to build this. If you don’t mind, please refactor my code and 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>
</UserControl>

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);
        }

    }
}


Thank you very much for share your code, it was very helpful for me to build my Grid with freeze Columns and Row. Hope this will help some other as well. You need to change the start positions of following items according to your layout, (scrollableGrid, hScrollBar, vScrollBar, colHeaderContainer, rowHeaderContainer )

        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();

        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);
                    TextBlock txt = new TextBlock();
                    txt.Text = "Row=" + j.ToString() + " Col=" + i.ToString();
                    sp.Children.Add(txt);
                    theGrid.Children.Add(sp);
                    theGrid.Children.Add(mbrd);
                }
            }

            gridContainer.Children.Add(theGrid);


            Canvas.SetLeft(scrollableGrid, 200);
            Canvas.SetTop(scrollableGrid, 100);
            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);                   

            SetScrollbars();


            AddColumnHeader();

            AddRowHeader();  

            this.LayoutRoot.Children.Add(firstRowfirstColCell);
            firstRowfirstColCell.BorderBrush = new SolidColorBrush(Colors.Black);
            firstRowfirstColCell.BorderThickness = new Thickness(1);
            firstRowfirstColCell.Width = 100;
            firstRowfirstColCell.Height = 50;
            firstRowfirstColCell.Background = new SolidColorBrush(Colors.Red);
            Canvas.SetLeft(firstRowfirstColCell, 0);
            Canvas.SetTop(firstRowfirstColCell, 0);


            theGrid.MouseWheel += theGrid_MouseWheel;

        }

        private void SetScrollbars()
        {
            hScrollBar.Orientation = Orientation.Horizontal;
            Canvas.SetLeft(hScrollBar, 200);
            Canvas.SetTop(hScrollBar, 450);
            hScrollBar.Width = 880;

            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, 1080);
            Canvas.SetTop(vScrollBar, 100);
            vScrollBar.Height = 350;

            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);
        }

        private void AddColumnHeader()
        {
            colHeaderContainer.Orientation = Orientation.Horizontal;

            //for (int i = 0; i <= 3; i++)
            //{
            //    Border mbrd = new Border();
            //    mbrd.BorderBrush = new SolidColorBrush(Colors.Red);
            //    mbrd.BorderThickness = new Thickness(1);
            //    mbrd.Width = 100;
            //    mbrd.Height = 50;
            //    colHeaderContainer.Children.Add(mbrd);
            //}

            Grid columnGrid = new Grid();
            columnGrid.ShowGridLines = true;
            for (int i = 0; i <= 10; i++)
            {
                ColumnDefinition cdef = new ColumnDefinition();
                cdef.Width = new GridLength(100);
                columnGrid.ColumnDefinitions.Add(cdef);
            }
            for (int i = 0; i <= 1; i++)
            {
                RowDefinition rdef = new RowDefinition();
                rdef.Height = new GridLength(50);
                columnGrid.RowDefinitions.Add(rdef);
            }
            for (int i = 0; i <= 10; i++)
            {
                for (int j = 0; j <= 1; 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.Yellow);
                    sp.SetValue(Grid.RowProperty, j);
                    sp.SetValue(Grid.ColumnProperty, i);
                    TextBlock txt = new TextBlock();
                    txt.Text = "Freez Col=" + i.ToString();
                    sp.Children.Add(txt);
                    columnGrid.Children.Add(sp);
                    columnGrid.Children.Add(mbrd);
                }
            }

            colHeaderContainer.Children.Add(columnGrid);


            colHeader.Children.Add(colHeaderContainer);
            Canvas.SetLeft(colHeader, 200);
            Canvas.SetTop(colHeader, 0);

            RectangleGeometry colHeaderrecClip = new RectangleGeometry();
            colHeaderrecClip.Rect = new Rect(0, 0, 880, 150);

            colHeader.Clip = colHeaderrecClip;


            this.LayoutRoot.Children.Add(colHeader);

        }

        private void AddRowHeader()
        {
            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);
            //}
            Grid rowGrid = new Grid();
            rowGrid.ShowGridLines = true;
            for (int i = 0; i <= 3; i++)
            {
                ColumnDefinition cdef = new ColumnDefinition();
                cdef.Width = new GridLength(100);
                rowGrid.ColumnDefinitions.Add(cdef);
            }
            for (int i = 0; i <= 10; i++)
            {
                RowDefinition rdef = new RowDefinition();
                rdef.Height = new GridLength(50);
                rowGrid.RowDefinitions.Add(rdef);
            }
            for (int i = 0; i <= 2; i++)
            {
                for (int j = 0; j <= 10; 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.Blue);
                    sp.SetValue(Grid.RowProperty, j);
                    sp.SetValue(Grid.ColumnProperty, i);
                    TextBlock txt = new TextBlock();
                    txt.Text = "Freez Row=" + i.ToString();
                    sp.Children.Add(txt);
                    rowGrid.Children.Add(sp);
                    rowGrid.Children.Add(mbrd);
                }
            }

            rowHeaderContainer.Children.Add(rowGrid);

            rowHeader.Children.Add(rowHeaderContainer);
            Canvas.SetLeft(rowHeader, 0);
            Canvas.SetTop(rowHeader, 100);

            RectangleGeometry rowHeaderrecClip = new RectangleGeometry();
            rowHeaderrecClip.Rect = new Rect(0, 0, 200, 350);

            rowHeader.Clip = rowHeaderrecClip;

            this.LayoutRoot.Children.Add(rowHeader);
        }

        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);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜