(WPF学习记录)第六章 Dock与Grid

来源:互联网 发布:python urllib2 open 编辑:程序博客网 时间:2024/05/18 03:43

 传统的程序具有相当标准的Layout,程序的菜单几乎是放在主窗口客户区的顶端,并延伸到整个窗口的宽度。如果程序有工具条,也是被dock(“停泊”)于客户区顶端,但是其中只有一个控件可以被停靠在最边缘。

WPF包含一个DockPanel类,可以满足不同的dock需求。

第01个小程序:DockPanel上的按钮(DockAroundTheBlock.cs)

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. namespace Chapter06
  7. {
  8.     class DockAroundTheBlock : Window
  9.     {
  10.         [STAThread]
  11.         public static void Main()
  12.         {
  13.             Application app = new Application();
  14.             app.Run(new DockAroundTheBlock());
  15.         }
  16.         public DockAroundTheBlock()
  17.         {
  18.             Title = "Dock Around the Block";
  19.             //Width = 384;
  20.             //Height = 288;
  21.             //SizeToContent = SizeToContent.WidthAndHeight;
  22.             DockPanel dock = new DockPanel();
  23.             //dock.LastChildFill = false;         //默认为true,即最后一个孩子不被Dock.
  24.             Content = dock;
  25.             for (int i = 0; i < 17; i++)
  26.             {
  27.                 Button btn = new Button();
  28.                 btn.Content = "按钮" + (i + 1);
  29.                 //btn.HorizontalAlignment = HorizontalAlignment.Center;
  30.                 if (i % 4 == 0)
  31.                     btn.Background = Brushes.Silver;
  32.                 if (i % 4 == 1)
  33.                     btn.Background = Brushes.SeaGreen;
  34.                 if (i % 4 == 2)
  35.                     btn.Background = Brushes.Salmon;
  36.                 if (i % 4 == 3)
  37.                     btn.Background = Brushes.RosyBrown;
  38.                 if (i == 16)
  39.                     btn.Background = Brushes.Pink;
  40.                 dock.Children.Add(btn);
  41.                 btn.SetValue(DockPanel.DockProperty, (Dock)(i % 4));
  42.             }
  43.         }
  44.     }
  45. }

此程序建立一个DockPanel,并创建17个按钮当作它的孩子。

btn.SetValue(DockPanel.DockProperty, (Dock)(i % 4))等同于btn.SetDock(ctrl, (Dock)(i % 4)),决定让ctrl控件dock在DockPanel的哪一边。(Dock)(i % 4)为每个按钮循环地指定Dock枚举值,依次为:Dock.Left、Dock.Top、Dock.Right和Dock.Bottom(它们的枚举值分别为0、1、2和3)。

最后加入的控件(按钮17)并没有被dock,但是会占据剩余的内部空间,这是因为DockPanel的LastChildFill属性默认为true所导致的。虽然程序有为最后一个按钮调用SetValue,但是这个值会被忽略。当加入“dock.LastChildFill = false;”这条语句时,就可以看到最后一个按钮也会继续被dock,因而留下一些空间。

当使用DockPanel时,远近要从外向内:第一个孩子具有紧贴着父亲边缘的优先权,后续的孩子会越来越向里。

DockPanel的孩子通常会扩张到版面全部的宽度(或高度),这是因为孩子的HorizontalAlignment和VerticalAlignment属性默认值都是Stretch(延展)。

运行结果:

第02个小程序:DockPanel的使用(MeetTheDockers.cs)

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Controls.Primitives;
  5. using System.Windows.Input;
  6. using System.Windows.Media;
  7. namespace Chapter06
  8. {
  9.     public class MeetTheDockers : Window
  10.     {
  11.         [STAThread]
  12.         public static void Main()
  13.         {
  14.             Application app = new Application();
  15.             app.Run(new MeetTheDockers());
  16.         }
  17.         public MeetTheDockers()
  18.         {
  19.             Title = "Meet the Dockers";
  20.             Height = 288;
  21.             Width = 384;
  22.             WindowStartupLocation = WindowStartupLocation.CenterScreen;
  23.             DockPanel dock = new DockPanel();
  24.             Content = dock;
  25.             // 创建菜单
  26.             Menu menu = new Menu();
  27.             MenuItem item = new MenuItem();
  28.             item.Header = "菜单";
  29.             menu.Items.Add(item);
  30.             // 将菜单dock在面板顶端
  31.             DockPanel.SetDock(menu, Dock.Top);
  32.             dock.Children.Add(menu);
  33.             // 创建工具栏
  34.             ToolBar tool = new ToolBar();
  35.             tool.Header = "工具栏";
  36.             // 将工具栏dock在面板顶端(菜单下面)
  37.             DockPanel.SetDock(tool, Dock.Top);
  38.             dock.Children.Add(tool);
  39.             // 创建状态栏
  40.             StatusBar status = new StatusBar();
  41.             StatusBarItem statitem = new StatusBarItem();
  42.             statitem.Content = "状态栏";
  43.             status.Items.Add(statitem);
  44.             // 将状态栏dock在面板底端
  45.             DockPanel.SetDock(status, Dock.Bottom);
  46.             dock.Children.Add(status);
  47.             // 创建list box.
  48.             ListBox lstbox = new ListBox();
  49.             lstbox.Items.Add("List Box 项");
  50.             // 将list box对象dock在面板左边
  51.             DockPanel.SetDock(lstbox, Dock.Left);
  52.             dock.Children.Add(lstbox);
  53.             // 创建text box.
  54.             TextBox txtbox = new TextBox();
  55.             txtbox.AcceptsReturn = true;
  56.             // 将text box加入面板,并让它具有输入焦点
  57.             dock.Children.Add(txtbox);
  58.             txtbox.Focus();
  59.         }
  60.     }
  61. }

这个程序创建了菜单、工具栏、状态栏、ListBox和TextBox控件,但没为它们加上实际的功能,只为演示使用DockPanel。

运行结果:

第03个小程序:计算你的年龄(CalculateYourLife.cs)

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. namespace Chapter06
  7. {
  8.     class CalculateYourLife : Window
  9.     {
  10.         TextBox txtboxBegin, txtboxEnd;
  11.         Label lblLifeYears;
  12.         [STAThread]
  13.         public static void Main()
  14.         {
  15.             Application app = new Application();
  16.             app.Run(new CalculateYourLife());
  17.         }
  18.         public CalculateYourLife()
  19.         {
  20.             Title = "计算你的年龄";
  21.             Height = 140;
  22.             Width = 240;
  23.             WindowStartupLocation = WindowStartupLocation.CenterScreen;
  24.             //SizeToContent = SizeToContent.WidthAndHeight;
  25.             ResizeMode = ResizeMode.CanMinimize;
  26.             // 创建grid面板
  27.             Grid grid = new Grid();
  28.             //grid.ShowGridLines = true;       //网格线可见。默认为false,不可见
  29.             Content = grid;
  30.             // 定义行和列
  31.             for (int i = 0; i < 3; i++)       //三行
  32.             {
  33.                 RowDefinition rowdef = new RowDefinition();
  34.                 rowdef.Height = GridLength.Auto;
  35.                 grid.RowDefinitions.Add(rowdef);
  36.             }
  37.             for (int i = 0; i < 2; i++)       //两列
  38.             {
  39.                 ColumnDefinition coldef = new ColumnDefinition();
  40.                 coldef.Width = GridLength.Auto;
  41.                 grid.ColumnDefinitions.Add(coldef);
  42.             }
  43.             // Label:出生日期
  44.             Label lbl = new Label();
  45.             lbl.Content = "出生日期: ";
  46.             grid.Children.Add(lbl);
  47.             Grid.SetRow(lbl, 0);
  48.             Grid.SetColumn(lbl, 0);
  49.             // TextBox:出生日期
  50.             txtboxBegin = new TextBox();
  51.             txtboxBegin.Width = 120;
  52.             txtboxBegin.Text = new DateTime(1111, 1, 1).ToShortDateString();
  53.             txtboxBegin.TextChanged += TextBoxOnTextChanged;
  54.             grid.Children.Add(txtboxBegin);
  55.             Grid.SetRow(txtboxBegin, 0);
  56.             Grid.SetColumn(txtboxBegin, 1);
  57.             // Label:当前日期
  58.             lbl = new Label();
  59.             lbl.Content = "当前日期: ";
  60.             grid.Children.Add(lbl);
  61.             Grid.SetRow(lbl, 1);
  62.             Grid.SetColumn(lbl, 0);
  63.             // TextBox::当前日期
  64.             txtboxEnd = new TextBox();
  65.             txtboxEnd.Width = 120;
  66.             txtboxEnd.Text = DateTime.Now.ToShortDateString();
  67.             txtboxEnd.TextChanged += TextBoxOnTextChanged;
  68.             grid.Children.Add(txtboxEnd);
  69.             Grid.SetRow(txtboxEnd, 1);
  70.             Grid.SetColumn(txtboxEnd, 1);
  71.             // Label:我的年龄
  72.             lbl = new Label();
  73.             lbl.Content = "我的年龄: ";
  74.             grid.Children.Add(lbl);
  75.             Grid.SetRow(lbl, 2);
  76.             Grid.SetColumn(lbl, 0);
  77.             // Label:计算结果
  78.             lblLifeYears = new Label();
  79.             grid.Children.Add(lblLifeYears);
  80.             Grid.SetRow(lblLifeYears, 2);
  81.             Grid.SetColumn(lblLifeYears, 1);
  82.             // 设置边界
  83.             Thickness thick = new Thickness(5); 
  84.             grid.Margin = thick;
  85.             foreach (Control ctrl in grid.Children)
  86.                 ctrl.Margin = thick;
  87.             // 设定焦点
  88.             txtboxBegin.Focus();
  89.             // 计算
  90.             Calculating();
  91.         }
  92.         // TextBox中的值改变后,计算年龄
  93.         void TextBoxOnTextChanged(object sender, TextChangedEventArgs args)
  94.         {
  95.             Calculating();   
  96.         }
  97.         // 计算年龄
  98.         private void Calculating()
  99.         {
  100.             DateTime dtBeg, dtEnd;
  101.             if (DateTime.TryParse(txtboxBegin.Text, out dtBeg) &
  102.                 DateTime.TryParse(txtboxEnd.Text, out dtEnd))
  103.             {
  104.                 int iYears = dtEnd.Year - dtBeg.Year;
  105.                 int iMonths = dtEnd.Month - dtBeg.Month;
  106.                 int iDays = dtEnd.Day - dtBeg.Day;
  107.                 if (iDays < 0)
  108.                 {
  109.                     iDays += DateTime.DaysInMonth(dtEnd.Year,
  110.                                             1 + (dtEnd.Month + 10) % 12);
  111.                     iMonths -= 1;
  112.                 }
  113.                 if (iMonths < 0)
  114.                 {
  115.                     iMonths += 12;
  116.                     iYears -= 1;
  117.                 }
  118.                 //lblLifeYears.Content =
  119.                 //    String.Format("{0} year{1}, {2} month{3}, {4} day{5}",
  120.                 //                  iYears, iYears == 1 ? "" : "s",
  121.                 //                  iMonths, iMonths == 1 ? "" : "s",
  122.                 //                  iDays, iDays == 1 ? "" : "s");
  123.                 lblLifeYears.Content =
  124.                     String.Format("{0}岁{1}个月零{2}天",
  125.                                   iYears,
  126.                                   iMonths,
  127.                                   iDays);
  128.             }
  129.             else
  130.             {
  131.                 lblLifeYears.Content = "";
  132.             }  
  133.         }
  134.     }
  135. }

Grid面板用行和列的格子状来显示其孩子,来实现行与列的控件布局。

使用GridLength类以指定行的高度和列的宽度。此类有两个构造函数:

new GridLength(100),这个值是独立单位,这样的构造函数等同于new GridLength(100, GridUnitType.Pixel)。也可以用GridUnitType.Star来当作第二个参数,如new GridLength(50, GridUnitType.Star),表示将剩余空间的50%分配给该行(列)。当用GridUnitType.Auto作为第二个参数时,如new GridLength(50, GridUnitType.Auto),这个数字会被忽略,等同静态的属性:GridLength.Auto。

Grid的静态方法SetRow和SetColumn方法,指定一个row和column索引值,以表示控件要摆放的位置。如:Grid.SetRow(lbl, 2),Grid.SetColumn(lbl, 0)。

程序运行结果如下:

第04个小程序:Grid布局(EnterTheGrid.cs)

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. namespace Chapter06
  7. {
  8.     public class EnterTheGrid : Window
  9.     {
  10.         [STAThread]
  11.         public static void Main()
  12.         {
  13.             Application app = new Application();
  14.             app.Run(new EnterTheGrid());
  15.         }
  16.         public EnterTheGrid()
  17.         {
  18.             Title = "又见Grid";
  19.             MinWidth = 300;       //最小宽度
  20.             WindowStartupLocation = WindowStartupLocation.CenterScreen;
  21.             SizeToContent = SizeToContent.WidthAndHeight;
  22.             // 创建StackPanel对象
  23.             StackPanel stack = new StackPanel();
  24.             Content = stack;
  25.             // 创建Grid并将其加入StackPanel
  26.             Grid grid1 = new Grid();
  27.             grid1.Margin = new Thickness(5);
  28.             stack.Children.Add(grid1);
  29.             // 定义行
  30.             for (int i = 0; i < 5; i++)
  31.             {
  32.                 RowDefinition rowdef = new RowDefinition();
  33.                 rowdef.Height = GridLength.Auto;
  34.                 grid1.RowDefinitions.Add(rowdef);
  35.             }
  36.             // 定义列
  37.             ColumnDefinition coldef = new ColumnDefinition();
  38.             coldef.Width = GridLength.Auto;
  39.             grid1.ColumnDefinitions.Add(coldef);
  40.             // 第2列
  41.             coldef = new ColumnDefinition();
  42.             coldef.Width = new GridLength(100, GridUnitType.Star);  //占据剩余空间
  43.             grid1.ColumnDefinitions.Add(coldef);
  44.             // 创建labels和text boxes.
  45.             string[] strLabels = { "姓:""名:",
  46.                                    "身份证号码:",
  47.                                    "信用卡号码:",
  48.                                    "其它:" };
  49.             for(int i = 0; i < strLabels.Length; i++)
  50.             {
  51.                 // 创建Label
  52.                 Label lbl = new Label();
  53.                 lbl.Content = strLabels[i];
  54.                 lbl.VerticalContentAlignment = VerticalAlignment.Center;
  55.                 grid1.Children.Add(lbl);
  56.                 Grid.SetRow(lbl, i);       //Label在第i+1行
  57.                 Grid.SetColumn(lbl, 0);    //Label在第1列
  58.                 // 创建TextBox
  59.                 TextBox txtbox = new TextBox();
  60.                 txtbox.Margin = new Thickness(5);
  61.                 grid1.Children.Add(txtbox);
  62.                 Grid.SetRow(txtbox, i);    //TextBox在第i+1行
  63.                 Grid.SetColumn(txtbox, 1); //TextBox在第2列
  64.             }
  65.             // 创建第二个Grid并将其加入StackPanel
  66.             Grid grid2 = new Grid();
  67.             grid2.Margin = new Thickness(10);
  68.             stack.Children.Add(grid2);
  69.             // 对于单一行,不需要row定义
  70.             // 默认列定义 “star”
  71.             grid2.ColumnDefinitions.Add(new ColumnDefinition());
  72.             grid2.ColumnDefinitions.Add(new ColumnDefinition());
  73.             // 创建按钮
  74.             Button btn = new Button();
  75.             btn.Content = "提交";
  76.             btn.HorizontalAlignment = HorizontalAlignment.Center;
  77.             btn.IsDefault = true;
  78.             //btn.Click += delegate { Close(); };
  79.             btn.Click += ButtonSubClick;
  80.             grid2.Children.Add(btn);    // 行号和列号都为0
  81.             btn = new Button();
  82.             btn.Content = "取消";
  83.             btn.HorizontalAlignment = HorizontalAlignment.Center;
  84.             btn.IsCancel = true;
  85.             //btn.Click += delegate { Close(); };
  86.             btn.Click += ButtonCanClick;
  87.             grid2.Children.Add(btn);
  88.             Grid.SetColumn(btn, 1);     //行0,列1
  89.             // 为第一个TextBox设置焦点
  90.             (stack.Children[0] as Panel).Children[1].Focus();
  91.         }
  92.         void ButtonSubClick(object sender, RoutedEventArgs args)
  93.         {
  94.             MessageBox.Show("提交成功!","个人信息");
  95.             this.Close();
  96.         }
  97.         void ButtonCanClick(object sender, RoutedEventArgs args)
  98.         {
  99.             MessageBox.Show("重新填写!""个人信息");
  100.         } 
  101.     }
  102. }

程序依赖元素默认的行为来填满他们的父亲。StackPanel占用窗口整个宽度,两个Grid面板也是。在最顶端的Grid面板中,第一列具有GridLength.Auto的宽度;第二列(有TextBox的列)的宽度为GridUnitType.Star式,即会占用所有剩余空间。所以当窗口变宽时,TextBox也会扩展宽度;当TextBox控件内文本超过其初始长度时,TextBox和窗口也会扩大。

运行结果:

第05个小程序:跨越单元格(SpanTheCells.cs)

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. namespace Chapter06
  7. {
  8.     public class SpanTheCells : Window
  9.     {
  10.         [STAThread]
  11.         public static void Main()
  12.         {
  13.             Application app = new Application();
  14.             app.Run(new SpanTheCells());
  15.         }
  16.         public SpanTheCells()
  17.         {
  18.             Title = "跨越单元格";
  19.             MinWidth = 300;       //最小宽度
  20.             WindowStartupLocation = WindowStartupLocation.CenterScreen;
  21.             SizeToContent = SizeToContent.WidthAndHeight;
  22.             // 创建Grid
  23.             Grid grid = new Grid();
  24.             grid.ShowGridLines = true;      //显示网格线
  25.             grid.Margin = new Thickness(5);
  26.             Content = grid;
  27.             // 定义行,6行
  28.             for (int i = 0; i < 6; i++)
  29.             {
  30.                 RowDefinition rowdef = new RowDefinition();
  31.                 rowdef.Height = GridLength.Auto;
  32.                 grid.RowDefinitions.Add(rowdef);
  33.             }
  34.             // 定义列,4列
  35.             for (int i = 0; i < 4; i++)
  36.             {
  37.                 ColumnDefinition coldef = new ColumnDefinition();
  38.                 // 第1列占据剩余空间,其余列自动排列
  39.                 if (i == 1)
  40.                     coldef.Width = new GridLength(100, GridUnitType.Star);
  41.                 else
  42.                     coldef.Width = GridLength.Auto;
  43.                 grid.ColumnDefinitions.Add(coldef);
  44.             }
  45.             // 创建labels和text boxes
  46.             string[] strLabels = { "姓:""名:",
  47.                                    "身份证号码:",
  48.                                    "信用卡号码:",
  49.                                    "其它:" };
  50.             for(int i = 0; i < strLabels.Length; i++)
  51.             {
  52.                 Label lbl = new Label();
  53.                 lbl.Content = strLabels[i];
  54.                 lbl.VerticalContentAlignment = VerticalAlignment.Center;
  55.                 grid.Children.Add(lbl);
  56.                 Grid.SetRow(lbl, i);       //Label放在第i+1行
  57.                 Grid.SetColumn(lbl, 0);    //Label放在第1列
  58.                 TextBox txtbox = new TextBox();
  59.                 txtbox.Margin = new Thickness(5);
  60.                 grid.Children.Add(txtbox);
  61.                 Grid.SetRow(txtbox, i);        //TextBox放在第i+1行
  62.                 Grid.SetColumn(txtbox, 1);     //TextBox放在第2列
  63.                 Grid.SetColumnSpan(txtbox, 3); //TextBox跨越到最后一列(第4列)
  64.             }
  65.             // 创建按钮
  66.             Button btn = new Button();
  67.             btn.Content = "提交";
  68.             btn.Margin = new Thickness(5);
  69.             btn.IsDefault = true;
  70.             btn.Click += delegate { Close(); };
  71.             grid.Children.Add(btn);
  72.             Grid.SetRow(btn, 5);     //"提交"按钮放在第6行
  73.             Grid.SetColumn(btn, 2);  //"提交"按钮放在第3列
  74.             btn = new Button();
  75.             btn.Content = "取消";
  76.             btn.Margin = new Thickness(5);
  77.             btn.IsCancel = true;
  78.             btn.Click += delegate { Close(); };
  79.             grid.Children.Add(btn);
  80.             Grid.SetRow(btn, 5);    //"取消"按钮放在第6行
  81.             Grid.SetColumn(btn, 3); //"取消"按钮放在第4列
  82.             // 为第一个TextBox设置焦点
  83.             grid.Children[1].Focus();
  84.         }
  85.     }
  86. }

这个程序和上一个相像,不同的是只使用一个6行4列的Grid面板,编辑框(TextBox)跨越占用2、3、4列。

指定元素占用的列的个数是用Grid.SetColumnSpan(txtbox, 3)实现的,类似占用行个数的设置方法是Grid.SetRowSpan(ctrl, 2)。

运行结果如下,这里设置显示Grid网格线,很容易看出控件布局格式:

第06个小程序:分隔线(SplitNine.cs)

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. namespace Chapter06
  7. {
  8.     public class SplitNine : Window
  9.     {
  10.         [STAThread]
  11.         public static void Main()
  12.         {
  13.             Application app = new Application();
  14.             app.Run(new SplitNine());
  15.         }
  16.         public SplitNine()
  17.         {
  18.             Title = "分隔线";
  19.             Grid grid = new Grid();
  20.             Content = grid;
  21.             // 定义行和列
  22.             for (int i = 0; i < 3; i++)
  23.             {
  24.                 grid.ColumnDefinitions.Add(new ColumnDefinition());
  25.                 grid.RowDefinitions.Add(new RowDefinition());
  26.             }
  27.             // 创建9个按钮
  28.             for (int x = 0; x < 3; x++)
  29.                 for (int y = 0; y < 3; y++)
  30.                 {
  31.                     Button btn = new Button();
  32.                     btn.Content = "第" + (y + 1) + "行 第" + (x + 1) + "列";
  33.                     btn.Margin = new Thickness(10);
  34.                     grid.Children.Add(btn);
  35.                     Grid.SetRow(btn, y);
  36.                     Grid.SetColumn(btn, x);
  37.                 }
  38.             // 创建splitter,指派其位置。此处位于第2行第2列处
  39.             GridSplitter split = new GridSplitter();
  40.             split.Width = 2;
  41.             split.Background = Brushes.Green;
  42.             grid.Children.Add(split);
  43.             Grid.SetRow(split, 1);
  44.             Grid.SetColumn(split, 1);
  45.         }
  46.     }
  47. }

运行后,在第2行第2列后会出现一条绿色的线,移动它时,不同行的列的宽度也会跟着改变。

运行结果如下:

第07个小程序:分隔客户区(SplitTheClient.cs)

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. using System.Windows.Media;
  6. namespace Chapter06
  7. {
  8.     class SplitTheClient : Window
  9.     {
  10.         [STAThread]
  11.         public static void Main()
  12.         {
  13.             Application app = new Application();
  14.             app.Run(new SplitTheClient());
  15.         }
  16.         public SplitTheClient()
  17.         {
  18.             Title = "分隔客户区";
  19.             Height = 288;
  20.             Width = 480;
  21.             WindowStartupLocation = WindowStartupLocation.CenterScreen;
  22.             // 具有垂直splitter的Grid
  23.             // 单行,无需设置row
  24.             // 3列:第1列放按钮1,第2列放垂直splitter,第3列放grid2
  25.             Grid grid1 = new Grid();
  26.             grid1.ColumnDefinitions.Add(new ColumnDefinition());
  27.             grid1.ColumnDefinitions.Add(new ColumnDefinition());
  28.             grid1.ColumnDefinitions.Add(new ColumnDefinition());
  29.             grid1.ColumnDefinitions[1].Width = GridLength.Auto;
  30.             Content = grid1;
  31.             // 垂直splitter左边的Button
  32.             Button btn = new Button();
  33.             btn.Content = "按钮1";
  34.             grid1.Children.Add(btn);
  35.             Grid.SetRow(btn, 0);
  36.             Grid.SetColumn(btn, 0);
  37.             // 垂直splitter.
  38.             GridSplitter split = new GridSplitter();
  39.             split.ShowsPreview = true;
  40.             split.Background = Brushes.Blue;
  41.             split.HorizontalAlignment = HorizontalAlignment.Center;
  42.             split.VerticalAlignment = VerticalAlignment.Stretch;
  43.             split.Width = 6;
  44.             grid1.Children.Add(split);
  45.             Grid.SetRow(split, 0);
  46.             Grid.SetColumn(split, 1);
  47.             // 具有水平splitter的Grid
  48.             // 3行:第1行放按钮2,第2行放水平splitter,第3行放按钮3
  49.             // 单列,无需设置column
  50.             Grid grid2 = new Grid();
  51.             grid2.RowDefinitions.Add(new RowDefinition());
  52.             grid2.RowDefinitions.Add(new RowDefinition());
  53.             grid2.RowDefinitions.Add(new RowDefinition());
  54.             grid2.RowDefinitions[1].Height = GridLength.Auto;
  55.             grid1.Children.Add(grid2);
  56.             Grid.SetRow(grid2, 0);
  57.             Grid.SetColumn(grid2, 2);
  58.             // 水平splitter上方的Button
  59.             btn = new Button();
  60.             btn.Content = "按钮2";
  61.             grid2.Children.Add(btn);
  62.             Grid.SetRow(btn, 0);
  63.             Grid.SetColumn(btn, 0);
  64.             // 水平splitter.
  65.             split = new GridSplitter();
  66.             split.ShowsPreview = true;
  67.             split.Background = Brushes.Blue;
  68.             split.HorizontalAlignment = HorizontalAlignment.Stretch;
  69.             split.VerticalAlignment = VerticalAlignment.Center;
  70.             split.Height = 6;
  71.             grid2.Children.Add(split);
  72.             Grid.SetRow(split, 1);
  73.             Grid.SetColumn(split, 0);
  74.             // 水平splitter底部的Button
  75.             btn = new Button();
  76.             btn.Content = "按钮3";
  77.             grid2.Children.Add(btn);
  78.             Grid.SetRow(btn, 2);
  79.             Grid.SetColumn(btn, 0);
  80.         }
  81.     }
  82. }

运行结果:

第08个小程序:颜色卷轴(ColorScroll.cs)

  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Controls.Primitives;
  5. using System.Windows.Media;
  6. namespace Chapter06
  7. {
  8.     class ColorScroll : Window
  9.     {
  10.         ScrollBar[] scrolls = new ScrollBar[3];
  11.         TextBlock[] txtValue = new TextBlock[3];
  12.         Panel pnlColor;
  13.         [STAThread]
  14.         public static void Main()
  15.         {
  16.             Application app = new Application();
  17.             app.Run(new ColorScroll());
  18.         }
  19.         public ColorScroll()
  20.         {
  21.             Title = "颜色卷轴";
  22.             Width = 500;
  23.             Height = 300;
  24.             WindowStartupLocation = WindowStartupLocation.CenterScreen;
  25.             // 包含垂直splitter的GridMain
  26.             Grid gridMain = new Grid();
  27.             //gridMain.ShowGridLines = true;
  28.             Content = gridMain;
  29.             // GridMain的列定义
  30.             // 3列:第1列放grid,第2列放垂直splitter,第3列放颜色面板pnlColor
  31.             ColumnDefinition coldef = new ColumnDefinition();
  32.             coldef.Width = new GridLength(200, GridUnitType.Pixel);
  33.             gridMain.ColumnDefinitions.Add(coldef);
  34.             coldef = new ColumnDefinition();
  35.             coldef.Width = GridLength.Auto;
  36.             gridMain.ColumnDefinitions.Add(coldef);
  37.             coldef = new ColumnDefinition();
  38.             coldef.Width = new GridLength(100, GridUnitType.Star);
  39.             gridMain.ColumnDefinitions.Add(coldef);
  40.             // 垂直splitter
  41.             GridSplitter split = new GridSplitter();
  42.             split.HorizontalAlignment = HorizontalAlignment.Center;
  43.             split.VerticalAlignment = VerticalAlignment.Stretch;
  44.             split.Width = 6;
  45.             gridMain.Children.Add(split);
  46.             Grid.SetRow(split, 0);
  47.             Grid.SetColumn(split, 1);
  48.             // splitter右边的面板,用来显示颜色
  49.             pnlColor = new StackPanel();
  50.             pnlColor.Background = new SolidColorBrush(SystemColors.WindowColor);
  51.             gridMain.Children.Add(pnlColor);
  52.             Grid.SetRow(pnlColor, 0);
  53.             Grid.SetColumn(pnlColor, 2);
  54.             // 左边的第2个Grid,将其加入gridMain
  55.             Grid grid = new Grid();
  56.             //grid.ShowGridLines = true;
  57.             gridMain.Children.Add(grid);
  58.             Grid.SetRow(grid, 0);
  59.             Grid.SetColumn(grid, 0);
  60.             // 定义3行,分别放置label,scroll和label
  61.             RowDefinition rowdef = new RowDefinition();
  62.             rowdef.Height = GridLength.Auto;
  63.             grid.RowDefinitions.Add(rowdef);
  64.             rowdef = new RowDefinition();
  65.             rowdef.Height = new GridLength(100, GridUnitType.Star);
  66.             grid.RowDefinitions.Add(rowdef);
  67.             rowdef = new RowDefinition();
  68.             rowdef.Height = GridLength.Auto;
  69.             grid.RowDefinitions.Add(rowdef);
  70.             // 3列:红、绿、蓝
  71.             for (int i = 0; i < 3; i++)
  72.             {
  73.                 coldef = new ColumnDefinition();
  74.                 coldef.Width = new GridLength(33, GridUnitType.Star);
  75.                 grid.ColumnDefinitions.Add(coldef);
  76.             }
  77.             for (int i = 0; i < 3; i++)
  78.             {
  79.                 Label lbl = new Label();
  80.                 lbl.Content = new string[] { "红""绿""蓝" }[i];
  81.                 lbl.HorizontalAlignment = HorizontalAlignment.Center;
  82.                 grid.Children.Add(lbl);
  83.                 Grid.SetRow(lbl, 0);
  84.                 Grid.SetColumn(lbl, i);
  85.                 scrolls[i] = new ScrollBar();
  86.                 scrolls[i].Focusable = true;
  87.                 scrolls[i].Orientation = Orientation.Vertical;
  88.                 scrolls[i].Minimum = 0;
  89.                 scrolls[i].Maximum = 255;
  90.                 scrolls[i].SmallChange = 1;
  91.                 scrolls[i].LargeChange = 16;
  92.                 scrolls[i].ValueChanged += ScrollOnValueChanged;
  93.                 grid.Children.Add(scrolls[i]);
  94.                 Grid.SetRow(scrolls[i], 1);
  95.                 Grid.SetColumn(scrolls[i], i);
  96.                 txtValue[i] = new TextBlock();
  97.                 txtValue[i].TextAlignment = TextAlignment.Center;
  98.                 txtValue[i].HorizontalAlignment = HorizontalAlignment.Center;
  99.                 txtValue[i].Margin = new Thickness(5);
  100.                 grid.Children.Add(txtValue[i]);
  101.                 Grid.SetRow(txtValue[i], 2);
  102.                 Grid.SetColumn(txtValue[i], i);
  103.             }
  104.             // 初始化卷轴
  105.             Color clr = (pnlColor.Background as SolidColorBrush).Color;
  106.             scrolls[0].Value = clr.R;
  107.             scrolls[1].Value = clr.G;
  108.             scrolls[2].Value = clr.B;
  109.             // 设置初始焦点
  110.             scrolls[0].Focus();
  111.         }
  112.         void ScrollOnValueChanged(object sender, RoutedEventArgs args)
  113.         {
  114.             ScrollBar scroll = sender as ScrollBar;
  115.             Panel pnl = scroll.Parent as Panel;
  116.             TextBlock txt = pnl.Children[1 +
  117.                                     pnl.Children.IndexOf(scroll)] as TextBlock;
  118.             txt.Text = String.Format("{0}/n0x{0:X2}", (int)scroll.Value);      //RGB和16进制显示
  119.             pnlColor.Background =
  120.                 new SolidColorBrush(
  121.                     Color.FromRgb((byte)scrolls[0].Value,
  122.                                   (byte)scrolls[1].Value, (byte)scrolls[2].Value));
  123.         }
  124.     }
  125. }

ScrollOnValueChanged事件处理器负责将ScroolBar的值更新到TextBlock,然后根据这3个滚动条的值计算出一个新的Color,显示在Color面板。

运行效果:

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 子母相伦动漫视频 离婚多年和儿子一起打工住一起没忍住 双子母视频 视频播放 与岳 母坐摩托车txt小说 我的丝母韵母txt 经典家庭伦txt丝母韵欲下载 经典家庭伦txt丝母韵欲阅读 经典家庭伦txt丝母韵欲全文阅读 笔趣阁丝母欲韵1-5 我的丝母韵欲txt下载 丝母欲韵1-5阅读 水利局的妈赵丽颖我的丝母欲韵 我的丝母欲韵4-9 经典家庭伦txt岳丝母小丹韵欲下载 水利局的妈赵丽静 我的丝母欲韵 岳丝母小丹韵 丝母欲韵txt全文下载80 经典家庭伦txt丝母韵欲光棍 李阳阳 白爽 丝母 我的丝母欲韵盘多多 娇母 笔趣阁 校长妈陈淑娴外篇 丝母欲韵500 我的丝母欲韵13 经典家庭伦txt丝母韵欲电子书 经典家庭伦txt丝母韵欲彩图 经典家庭伦txt丝母韵欲txt下载 经典家庭伦txt丝母韵欲d下载 经典家庭伦txt丝母韵欲视频 长篇都市情感小说 经典家庭伦全文 久九九久精品免费视频 91在线中文字幕香蕉人人视频 经典家庭伦txt岳丝母小丹韵欲阅读 碰超上线视频人人视频千人 91青青碰起上线视频人人 车里太挤姑妈坐我腿上做小说 经典家庭伦txt岳丝母小丹韵欲小说 我的丝母欲韵第六节 91在线中文久人人视频动漫 悉母全集动漫完整视频