具有固定标题和列的Silverlight自定义网格,需要重构

时间:2010-10-23 04:48:23

标签: silverlight refactoring grid

我正在尝试使用Silverlight中的固定标题和列构建自定义网格。我不知道SL是否具有构建此功能的内置功能,因为我对SL非常新。请查看以下代码。我不认为这是建立这个的正确方法。如果您不介意,请重构我的代码并以正确的方式告诉我。

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

    }
}

1 个答案:

答案 0 :(得分:0)

非常感谢您分享您的代码,使用冻结列和行构建我的网格非常有帮助。希望这也有助于其他一些人。 您需要根据布局更改以下项目的起始位置, (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);
        }