silverlight 自定義表格

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

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<string, Rectangle> Rects 保存矩形信息集合

public Dictionary<string, TextBlock> 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";

代碼下載路徑:http://download.csdn.net/detail/lijun7788/4659651

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章