《WPF程序设计指南》读书笔记——第6章 Dock与Grid_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 《WPF程序设计指南》读书笔记——第6章 Dock与Grid

《WPF程序设计指南》读书笔记——第6章 Dock与Grid

 2013/8/25 22:55:09  小p  博客园  我要评论(0)
  • 摘要:1.DockPanel面板usingSystem;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Input;usingSystem.Windows.Media;namespaceLY.DockAroundTheBlock{classDockAroundTheBlock:Window{[STAThread]publicstaticvoidMain(){newApplication().Run
  • 标签:程序 笔记 读书笔记 设计

1.DockPanel面板

class="brush:csharp;collapse:true;;gutter:true;">using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace LY.DockAroundTheBlock
{
    class DockAroundTheBlock:Window
    {
        [STAThread]
        public static void Main()
        {
            new Application().Run(new DockAroundTheBlock());
        }
        public DockAroundTheBlock()
        {
            Title = "Dock Around The Block";
            DockPanel dock = new DockPanel();
            //LastChildFill属性默认值为true
            //dock.LastChildFill = false;
            Content = dock;
            for (int i = 0; i < 17; i++)
            {
                Button btn = new Button();
                btn.Content = "Button No. " + (i + 1);
                dock.Children.Add(btn);
                //将数值直接转换为枚举
                //以下两种方法都可以
                //btn.SetValue(DockPanel.DockProperty, (Dock)(i % 4));
                DockPanel.SetDock(btn, (Dock)(i % 4));
            }
        }
    }
}

2.Grid面板  

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace LY.CalculateYourLife
{
    class CalculateYourLife : Window
    {
        TextBox txtboxBegin, txtboxEnd;
        Label lblLifeYears;

        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new CalculateYourLife());
        }
        public CalculateYourLife()
        {
            Title = "Calculate Your Life";
            SizeToContent = SizeToContent.WidthAndHeight;
            ResizeMode = ResizeMode.CanMinimize;
 
            Grid grid = new Grid();
            Content = grid;
            //显示Grid面板的网格线
            grid.ShowGridLines = true;
            // 行和列的定义
            for (int i = 0; i < 3; i++)
            {
                //RowDefinitions是RowDefinition的集合
                //用于定义网格的行
                RowDefinition rowdef = new RowDefinition();
                /*
                 * GridLength是结构体,可以用GridLength.Auto静态属性定义行的高度,如果
                 *用GridLength.Pixel(固定大小)或GridLength.Star(可用空间百分比)枚举
                 *也可以用new GridLength()构造函数,来指定固定大小或分配权重
                 *如果在new GridLength()构造函数中用GridLength.Auto枚举,则会忽略数值
                */
                rowdef.Height = GridLength.Auto;//根据内容自动设定高度
                //rowdef.Height = new GridLength(100, GridUnitType.Pixel);
                //Grid会将所有GridUnitType.Star枚举值加起来得到总和,然后
                //让每个值除以总和,以决定空间分配比
                //rowdef.Height = new GridLength(20, GridUnitType.Star);
                //如果用无参数构造函数,则默认为GridUnitType.Star,权重值为1
         //rowdef.Height = new GridLength();   
                grid.RowDefinitions.Add(rowdef);
            }

            for (int i = 0; i < 2; i++)
            {
                ColumnDefinition coldef = new ColumnDefinition();
                coldef.Width = GridLength.Auto;
                grid.ColumnDefinitions.Add(coldef);
            }

            // First label.
            Label lbl = new Label();
            lbl.Content = "Begin Date: ";
            grid.Children.Add(lbl);
            //将控件放到Grid面板中
            Grid.SetRow(lbl, 0);
            Grid.SetColumn(lbl, 0);

            // First TextBox.
            txtboxBegin = new TextBox();
            txtboxBegin.Text = new DateTime(1980, 1, 1).ToShortDateString();
            txtboxBegin.TextChanged += TextBoxOnTextChanged;
            grid.Children.Add(txtboxBegin);
            Grid.SetRow(txtboxBegin, 0);
            Grid.SetColumn(txtboxBegin, 1);

            // Second label.
            lbl = new Label();
            lbl.Content = "End Date: ";
            grid.Children.Add(lbl);
            Grid.SetRow(lbl, 1);
            Grid.SetColumn(lbl, 0);

            // Second TextBox.
            txtboxEnd = new TextBox();
            txtboxEnd.TextChanged += TextBoxOnTextChanged;
            grid.Children.Add(txtboxEnd);
            Grid.SetRow(txtboxEnd, 1);
            Grid.SetColumn(txtboxEnd, 1);

            // Third label.
            lbl = new Label();
            lbl.Content = "Life Years: ";
            grid.Children.Add(lbl);
            Grid.SetRow(lbl, 2);
            Grid.SetColumn(lbl, 0);

            // Label for calculated result.
            lblLifeYears = new Label();
            grid.Children.Add(lblLifeYears);
            Grid.SetRow(lblLifeYears, 2);
            Grid.SetColumn(lblLifeYears, 1);

            // Set margin for everybody.
            Thickness thick = new Thickness(5);
            grid.Margin = thick;
            //遍历控件
            foreach (Control ctrl in grid.Children)
                ctrl.Margin = thick;

            txtboxBegin.Focus();
            txtboxEnd.Text = DateTime.Now.ToShortDateString();
        }
        void TextBoxOnTextChanged(object sender, TextChangedEventArgs args)
        {
            DateTime dtBeg, dtEnd;

            if (DateTime.TryParse(txtboxBegin.Text, out dtBeg) &&
                DateTime.TryParse(txtboxEnd.Text, out dtEnd))
            {
                int iYears = dtEnd.Year - dtBeg.Year;
                int iMonths = dtEnd.Month - dtBeg.Month;
                int iDays = dtEnd.Day - dtBeg.Day;

                if (iDays < 0)
                {
                    iDays += DateTime.DaysInMonth(dtEnd.Year,
                                            1 + (dtEnd.Month + 10) % 12);
                    iMonths -= 1;
                }
                if (iMonths < 0)
                {
                    iMonths += 12;
                    iYears -= 1;
                }
                lblLifeYears.Content =
                    String.Format("{0} year{1}, {2} month{3}, {4} day{5}",
                                  iYears, iYears == 1 ? "" : "s",
                                  iMonths, iMonths == 1 ? "" : "s",
                                  iDays, iDays == 1 ? "" : "s");
            }
            else
            {
                lblLifeYears.Content = "";
            }
        }
    }
}

 

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace LY.EnterTheGrid
{
    public class EnterTheGrid : Window
    {
        [STAThread]
        public static void Main()
        {
            Application app = new Application();
            app.Run(new EnterTheGrid());
        }
        public EnterTheGrid()
        {
            Title = "Enter the Grid";
            MinWidth = 300;
            SizeToContent = SizeToContent.WidthAndHeight;

            //新建一个StackPanel
            StackPanel stack = new StackPanel();
            Content = stack;

            //新建Grid面板,并且加入到StackPanel
            Grid grid1 = new Grid();
            grid1.Margin = new Thickness(5);
            stack.Children.Add(grid1);

            //设定行
            for (int i = 0; i < 5; i++)
            {
                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = GridLength.Auto;
                grid1.RowDefinitions.Add(rowdef);
            }

            //设定列
            ColumnDefinition coldef = new ColumnDefinition();
            coldef.Width = GridLength.Auto;
            grid1.ColumnDefinitions.Add(coldef);
            coldef = new ColumnDefinition();
            //第二列会随着边框拉大而自动变大,因为其GridUnitType为Star
            coldef.Width = new GridLength(100, GridUnitType.Star);
            grid1.ColumnDefinitions.Add(coldef);

            //创建labels控件
            string[] strLabels = { "_First name:", "_Last name:",
                                   "_Social security number:",
                                   "_Credit card number:",
                                   "_Other personal stuff:" };

            for(int i = 0; i < strLabels.Length; i++)
            {
                Label lbl = new Label();
                lbl.Content = strLabels[i];
                lbl.VerticalContentAlignment = VerticalAlignment.Center;
                grid1.Children.Add(lbl);
                Grid.SetRow(lbl, i);
                Grid.SetColumn(lbl, 0);

                TextBox txtbox = new TextBox();
                txtbox.Margin = new Thickness(5);
                grid1.Children.Add(txtbox);
                Grid.SetRow(txtbox, i);
                Grid.SetColumn(txtbox, 1);
            }

            //再新建一个Grid面板,并且加入到StackPanel
            Grid grid2 = new Grid();
            grid2.Margin = new Thickness(10);
            stack.Children.Add(grid2);

            // 因为grid2只需要有一行,因此不用定义行
            // 默认方式下是Star枚举,权重值为1
            grid2.ColumnDefinitions.Add(new ColumnDefinition());
            grid2.ColumnDefinitions.Add(new ColumnDefinition());
            grid2.ColumnDefinitions.Add(new ColumnDefinition());
            grid2.ShowGridLines = true;
            //加入按钮
            Button btn = new Button();
            btn.Content = "Submit";
            btn.HorizontalAlignment = HorizontalAlignment.Center;
            btn.IsDefault = true;
            btn.Click += delegate { Close(); };
            grid2.Children.Add(btn);    // 默认情况下行和列都是0
            Grid.SetColumnSpan(btn, 2); // 跨两个列

            btn = new Button();
            btn.Content = "Cancel";
            btn.HorizontalAlignment = HorizontalAlignment.Center;
            btn.IsCancel = true;
            btn.Click += delegate { Close(); };
            grid2.Children.Add(btn);
            Grid.SetColumn(btn, 2);     //默认行是0.

            //设定激活控件
            (stack.Children[0] as Panel).Children[1].Focus();
       }
    }
}

  

 

上一篇: Android使用Java Mail API发送邮件 下一篇: 没有下一篇了!
发表评论
用户名: 匿名