Silverlight 自訂表格格 轉

來源:互聯網
上載者:User

在項目中可能用到如下表格式結構: DataGrid綁定好象沒有此功能,因此自己定義了一個MyGrid代碼如下: 自己定義一個UserControl,在其中添加一人Grid控制項然後設定行和列如下: UserControlx:Class= Hahaman.UI.MyGrid xmlns= http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x= http://schemas.microsoft.c  

  在項目中可能用到如下表格式結構:

  

 

  DataGrid綁定好象沒有此功能,因此自己定義了一個MyGrid代碼如下:

  自己定義一個UserControl,在其中添加一人Grid控制項然後設定行和列如下:

<UserControl x:Class="Hahaman.UI.MyGrid"
    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="88" d:DesignWidth="566">
    
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="40*" />
            <RowDefinition Height="40*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />
            <ColumnDefinition Width="10*" />            
            <ColumnDefinition Width="10*" />
        </Grid.ColumnDefinitions>
    </Grid>
</UserControl>

  在控制項代碼中添加三個屬性:

  public Dictionary Rects 儲存矩形資訊集合

  public Dictionary Texts 儲存TextBlock控制項集合

  public int Cols 儲存列數

  

 

  添加第一列矩形框的代碼:

            Rectangle r1;
            r1= new Rectangle();
            r1.SetValue(Grid.RowSpanProperty, 2);
            r1.SetValue(Grid.ColumnProperty, 0);
            r1.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
            r1.StrokeThickness = 1;
            LayoutRoot.Children.Add(r1);

  添加第一列文字框的代碼:

            TextBlock txt = new TextBlock();
            txt.SetValue(Grid.RowSpanProperty, 2);
            txt.SetValue(Grid.ColumnProperty, 0);
            txt.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            Texts.Add("0,0", txt);
            LayoutRoot.Children.Add(txt);

  添加其它的列:

        void AddLine()
        {
            Rectangle r1;
            TextBlock txt;
            int n = LayoutRoot.ColumnDefinitions.Count - 1;
            for (int i = 1; i <=n; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    r1 = new Rectangle();
                    r1.SetValue(Grid.RowProperty, j);
                    r1.SetValue(Grid.ColumnProperty, i);
                    r1.Stroke = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
                    r1.Margin = new Thickness(i > 0 ? -1 : 0, j > 0 ? -1 : 0, 0, 0);
                    r1.StrokeThickness = 1;
                    LayoutRoot.Children.Add(r1);
                    Rects.Add(i + "," + j, r1);

                    txt = new TextBlock();
                    txt.SetValue(Grid.RowProperty, j);
                    txt.SetValue(Grid.ColumnProperty, i);
                    txt.Margin = new Thickness(3,0,0,0);
                    txt.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    LayoutRoot.Children.Add(txt);
                    Texts.Add(i+ "," + j, txt);
                }
            }
        }

  當Cols改變時需要重新繪製:

        public int Cols
        {
            get
            {
                return LayoutRoot.ColumnDefinitions.Count - 1;
            }
            set
            {
                var old=LayoutRoot.ColumnDefinitions.Count - 1;
                if (value > old)
                {
                    for (int i = old; i < value; i++)
                    {
                        LayoutRoot.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength ( 10, GridUnitType.Star ) });
                        
                    }
                    
                }
                else
                {
                    for (int i = 0; i < old - value; i++)
                    {
                        LayoutRoot.ColumnDefinitions.RemoveAt(value);                        
                    }

                }
                ReDraw();
            }
        }

  這樣設計時修改列數時就可以自動更新列數,如:

  

 

  前台控制碼:

            var s = new SolidColorBrush();
            s.SetValue(SolidColorBrush.ColorProperty,Colors.LightGray);
            myGrid1.Rects["4,0"].Fill = s;
            myGrid1.Rects["4,1"].Fill = s;
            myGrid1.Texts["0,0"].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            myGrid1.Texts["0,0"].Text = "data";
            myGrid1.Texts["1,0"].Text = "data1";
            myGrid1.Texts["1,1"].Text = "data2";
源碼下載

  本文來自lijun7788的部落格,原文地址:http://blog.csdn.net/lijun7788/article/details/8085017

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.