久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

Silverlight中使用Grid創(chuàng)建自定義的Table表格

 A_POST 2014-01-04

  在Silverlight沒有像HTML中的table標(biāo)簽,,如果我們想創(chuàng)建一個(gè)類似表格的界面或者說想創(chuàng)建一個(gè)類似Excel表格,那該如何創(chuàng)建呢,,對(duì)于一般的可以使用DataGrid控件實(shí)現(xiàn)表格的顯示,,但是如果出現(xiàn)跨行跨列需要合并的表格,那DataGrid就無法實(shí)現(xiàn)了。接下來,,我就介紹,,如何使用Grid控件實(shí)現(xiàn)這樣的功能。

   首先,,對(duì)于Gird布局控件,,我們知道它有行和列的屬性,以及可以在每行每列的單元格中放入控件,。首先我們需要知道我們創(chuàng)建的表格有多少行或多少列,,然后我們?cè)趧?dòng)態(tài)創(chuàng)建行和列。

   /// <summary>
        /// 創(chuàng)建Grid,實(shí)現(xiàn)表格的主體
        /// </summary>
        protected  void CreateGrid(int rows,int columns)
        {
            grid = new Grid()
                    {
                        Background = null,
                        HorizontalAlignment = HorizontalAlignment.Stretch,
                        VerticalAlignment = VerticalAlignment.Top,
                        Margin = new Thickness(10, 0, 10, 0)
                    };
            for (int i = 0; i < rows; i++)
            {
                RowDefinition row = CreateRow();
                grid.RowDefinitions.Add(row);
            }

            for (int i = 0; i < columns; i++)
            {
                ColumnDefinition column = CreateColumn();
                grid.ColumnDefinitions.Add(column);
            }
        }
復(fù)制代碼自定義創(chuàng)建行和列的方法:CreateRow()和CreateColumn():
復(fù)制代碼
     /// <summary>
        /// 創(chuàng)建表格的一行
         /// </summary>
        /// <returns>返回Grid的一行</returns>
        private RowDefinition CreateRow()
        {
            RowDefinition row = new RowDefinition()
                                  {
                                      Height = new GridLength(1, GridUnitType.Star)
                                  };
            return row;
        }

        /// <summary>
        /// 創(chuàng)建表格的一列
        /// </summary>
        /// <returns>返回Grid的一列</returns>
        private ColumnDefinition CreateColumn()
        {
            ColumnDefinition column = new ColumnDefinition()
                                          {
                                              Width = new GridLength(1, GridUnitType.Star)
                                          };
            return column;
        }
復(fù)制代碼

  上面的代碼,,實(shí)現(xiàn)了一個(gè)表格的框架的創(chuàng)建,,沒有邊框沒有內(nèi)容,接下來我們創(chuàng)建它的表頭以及邊框:

復(fù)制代碼
        protected virtual void CreateHeader(List<string> headList)
        {
            for (int i = 0; i < subHeadList.Count; i++)
            {
                TextBlock block = CreateTextBlock(headList[i]);
                Border border = new Border() { Background = new SolidColorBrush(Colors.Transparent), BorderThickness = new Thickness(1, 1, 1, 1), BorderBrush = ConvertColor("FFC9CACA") };
                border.Child = block;
                border.SetValue(Grid.RowProperty, 0);
                border.SetValue(Grid.ColumnProperty, i);
                grid.Children.Add(border); 
            }
        }
復(fù)制代碼

    創(chuàng)建Border是為了讓單元格有邊框,,上面創(chuàng)建的Border的寬度為1Px,,創(chuàng)建完后,你會(huì)發(fā)現(xiàn)最外層的邊框?qū)挾葧?huì)細(xì)一些,,中間內(nèi)容的邊框會(huì)粗一些,,這是因?yàn)橹虚g的部分是兩個(gè)Border的寬度,如果要追求完美,,你可以將表格最外層單元格的Border的寬度逐一設(shè)置一下。

    創(chuàng)建TextBlock的方法,,創(chuàng)建TextBlock主要是用來顯示表格里面的內(nèi)容:    

復(fù)制代碼
/// <summary>
        /// 創(chuàng)建一個(gè)TextBlock控件
        /// </summary>
        /// <param name="text">控件的Text</param>
        /// <returns></returns>
        protected TextBlock CreateTextBlock(string text)
        {
            TextBlock block = new TextBlock()
            {
                Text = text,
                FontSize = 12,
                Foreground = new SolidColorBrush(Colors.Black),
                Margin = new Thickness(0, 5, 0, 5),
                TextWrapping = TextWrapping.Wrap,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };
            return block;
        }
復(fù)制代碼

 

    顏色轉(zhuǎn)換的方法ConvertColor():

復(fù)制代碼
/// <summary>
        /// 將ARGB顏色轉(zhuǎn)化成系統(tǒng)的顏色
        /// </summary>
        /// <param name="color">ARGB色彩的字符串</param>
        /// <returns></returns>
        protected SolidColorBrush ConvertColor(string color)
        {
            SolidColorBrush brush;
            try
            {
                byte a, r, g, b;
                a = r = g = b = 255;
                r = Convert.ToByte(color.Substring(0, 2), 16);
                g = Convert.ToByte(color.Substring(2, 2), 16);
                b = Convert.ToByte(color.Substring(4, 2), 16);
                brush = new SolidColorBrush(Color.FromArgb(a, b, g, r));
                return brush;
            }
            catch
            {
                return new SolidColorBrush(Colors.Black); 
            }
        }
復(fù)制代碼

    以上就完成了表格的表頭的創(chuàng)建,,下面介紹一下有關(guān)表頭需要合并的操作:  

復(fù)制代碼
        /// <summary>
        /// 創(chuàng)建表格的表頭
        /// </summary>
        protected virtual void CreateHeaderOne()
        {
            TextBlock block;
            block=CreateTextBlock(headList[0]);
            Border border =new Border(){ Background=new SolidColorBrush(Colors.Transparent),BorderThickness=new Thickness(2,2,1,1) ,BorderBrush = ConvertColor("FFC9CACA")};                
            border.Child = block;
            border.SetValue(Grid.RowProperty,0);
            border.SetValue(Grid.ColumnProperty, 0);
            grid.Children.Add(border);

            block = CreateTextBlock(headList[1]);
            border = new Border() { Background = new SolidColorBrush(Colors.Transparent), BorderThickness = new Thickness(1, 2, 1, 1), BorderBrush = ConvertColor("FFC9CACA") };
            border.Child = block;
            border.SetValue(Grid.RowProperty, 0);
            border.SetValue(Grid.ColumnProperty, 1);
            border.SetValue(Grid.ColumnSpanProperty, 2);//合并兩列
            grid.Children.Add(border);

            block = CreateTextBlock(headList[2]);
            border = new Border() { Background = new SolidColorBrush(Colors.Transparent), BorderThickness = new Thickness(1, 2, 1, 1), BorderBrush = ConvertColor("FFC9CACA") };
            border.Child = block;
            border.SetValue(Grid.RowProperty, 0);
            border.SetValue(Grid.ColumnProperty, 3);    
            border.SetValue(Grid.RowSpanProperty, 2);//合并兩行
            grid.Children.Add(border);           
          
        }
復(fù)制代碼

   表格內(nèi)容的填充:

    

復(fù)制代碼
/// <summary>
        /// 將數(shù)據(jù)綁定到表格里
        /// </summary>
        protected virtual void BindingContent()
        {
            int index = 1;
            foreach (var data in dataSource)
            {
                BindingOneColumn(index, data);
                index++;
            }
        }
        /// <summary>
        /// 綁定一行數(shù)據(jù)
        /// </summary>
        private void BindingOneColumn(int index,Model data)
        {
            TextBlock block;
            block=CreateTextBlock(data.ID.ToString());
            Border border =new Border(){ Background=new SolidColorBrush(Colors.Transparent),BorderThickness=new Thickness(1,1,1,1) ,BorderBrush = ConvertColor("FFC9CACA")};                
            border.Child = block;
            border.SetValue(Grid.RowProperty,index);
            border.SetValue(Grid.ColumnProperty, 0);
            grid.Children.Add(border);

            string text = ConverterToString(data.Name);
            block = CreateTextBlock(text);
            border = new Border() { Background = new SolidColorBrush(Colors.Transparent), BorderThickness = new Thickness(1, 1, 1, 1), BorderBrush = ConvertColor("FFC9CACA") };
            border.Child = block;
            border.SetValue(Grid.RowProperty, index);
            border.SetValue(Grid.ColumnProperty, 1);
            grid.Children.Add(border);

            text = ConverterToString(data.Sex);
            block = CreateTextBlock(text);
            border = new Border() { Background = new SolidColorBrush(Colors.Transparent), BorderThickness = new Thickness(1, 1, 1, 1), BorderBrush = ConvertColor("FFC9CACA") };
            border.Child = block;
            border.SetValue(Grid.RowProperty, index);
            border.SetValue(Grid.ColumnProperty, 2);
            grid.Children.Add(border);


            text = ConverterToString(data.Age);
            block = CreateTextBlock(text);
            border = new Border() { Background = new SolidColorBrush(Colors.Transparent), BorderThickness = new Thickness(1, 1, 1, 1), BorderBrush = ConvertColor("FFC9CACA") };
            border.Child = block;
            border.SetValue(Grid.RowProperty, index);
            border.SetValue(Grid.ColumnProperty, 3);
            grid.Children.Add(border);

            text = ConverterToString(data.Address);
            block = CreateTextBlock(text);
            border = new Border() { Background = new SolidColorBrush(Colors.Transparent), BorderThickness = new Thickness(1, 1, 1, 1), BorderBrush = ConvertColor("FFC9CACA") };
            border.Child = block;
            border.SetValue(Grid.RowProperty, index);
            border.SetValue(Grid.ColumnProperty, 4);
            grid.Children.Add(border);
           
        }
復(fù)制代碼

  以上代碼實(shí)現(xiàn)了,將我們的數(shù)據(jù)集合循環(huán)添加到Gird的每一行,,每一列中去,。其中的dataSource是我們的數(shù)據(jù)集合,它是我們自定義類型Model的集合,。

  以上就完成了通過Grid控件實(shí)現(xiàn)Table的創(chuàng)建,,效果圖,如下所示:

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約