《WPF程序设计指南》读书笔记——第6章 Dock与Grid
1.DockPanel面板
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();
}
}
}
3.GridSplitter控件(只能在Grid面板中使用)
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media; namespace LY.SplitNine
{
class SplitNine:Window
{
[STAThread]
public static void Main()
{
new Application().Run(new SplitNine());
}
public SplitNine()
{
Title = "Split Nine";
Grid grid = new Grid();
grid.ShowGridLines = true;
Content = grid;
for (int i = 0; i < 3; i++)
{
grid.RowDefinitions.Add(new RowDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Button btn = new Button();
btn.Content = "行" + j + "列" + i;
btn.Margin = new Thickness(5);
grid.Children.Add(btn);
//Grid类静态方法设定控件在Grid面板上位置
Grid.SetRow(btn, j);
Grid.SetColumn(btn, i);
}
}
GridSplitter split = new GridSplitter();
//是否同时显示拖动前的split控件位置
split.ShowsPreview = true;
//split默认在网格cell中靠右伸展
//水平分隔控件
split.HorizontalAlignment = HorizontalAlignment.Stretch;
split.VerticalAlignment = VerticalAlignment.Top;
//垂直分隔控件
//split.HorizontalAlignment = HorizontalAlignment.Center;
//split.VerticalAlignment = VerticalAlignment.Stretch;
//拖动split会影响到哪些行和列
split.ResizeBehavior = GridResizeBehavior.CurrentAndNext;
//拖动split会影响到行还是列
split.ResizeDirection = GridResizeDirection.Rows;
//split默认宽度和高度为0,所以必须设一个值
//注意:水平分隔时,不能只设width(Stretch属性设定了)不设Heigth
//注意:垂直分隔时,不能只设Heigth(Stretch属性设定了)不设width
//注意:否则分割线没有Heigth或width都会显示不出来
split.Width = 30;
split.Height = 20;
split.Background = Brushes.Red;
split.Margin = new Thickness(10);
grid.Children.Add(split);
Grid.SetRow(split, 1);
Grid.SetColumn(split, 1);
//Grid.SetRowSpan(split,3);
}
}
}
4.ScrollBar控件
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media; class ColorScroll : Window
{
ScrollBar[] scrolls = new ScrollBar[3];
TextBlock[] txtValue = new TextBlock[3];
Panel pnlColor; [STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new ColorScroll());
}
public ColorScroll()
{
Title = "Color Scroll";
Width = 500;
Height = 300; // GridMain contains a vertical splitter.
Grid gridMain = new Grid();
Content = gridMain; // GridMain column definitions.
ColumnDefinition coldef = new ColumnDefinition();
coldef.Width = new GridLength(200, GridUnitType.Pixel);
gridMain.ColumnDefinitions.Add(coldef); coldef = new ColumnDefinition();
coldef.Width = GridLength.Auto;
gridMain.ColumnDefinitions.Add(coldef); coldef = new ColumnDefinition();
coldef.Width = new GridLength(100, GridUnitType.Star);
gridMain.ColumnDefinitions.Add(coldef); // Vertical splitter.
GridSplitter split = new GridSplitter();
split.HorizontalAlignment = HorizontalAlignment.Center;
split.VerticalAlignment = VerticalAlignment.Stretch;
split.Width = 6;
gridMain.Children.Add(split);
Grid.SetRow(split, 0);
Grid.SetColumn(split, 1); // Panel on right side of splitter to display color
pnlColor = new StackPanel();
pnlColor.Background = new SolidColorBrush(SystemColors.WindowColor);
gridMain.Children.Add(pnlColor);
Grid.SetRow(pnlColor, 0);
Grid.SetColumn(pnlColor, 2); // Secondary grid at left of splitter
Grid grid = new Grid();
gridMain.Children.Add(grid);
Grid.SetRow(grid, 0);
Grid.SetColumn(grid, 0); // Three rows for label, scroll, and label.
RowDefinition rowdef = new RowDefinition();
rowdef.Height = GridLength.Auto;
grid.RowDefinitions.Add(rowdef); rowdef = new RowDefinition();
rowdef.Height = new GridLength(100, GridUnitType.Star);
grid.RowDefinitions.Add(rowdef); rowdef = new RowDefinition();
rowdef.Height = GridLength.Auto;
grid.RowDefinitions.Add(rowdef); // Three columns for Red, Green, and Blue.
for (int i = 0; i < 3; i++)
{
coldef = new ColumnDefinition();
coldef.Width = new GridLength(33, GridUnitType.Star);
grid.ColumnDefinitions.Add(coldef);
} for (int i = 0; i < 3; i++)
{
Label lbl = new Label();
lbl.Content = new string[] { "Red", "Green", "Blue" }[i];
lbl.HorizontalAlignment = HorizontalAlignment.Center;
grid.Children.Add(lbl);
Grid.SetRow(lbl, 0);
Grid.SetColumn(lbl, i); scrolls[i] = new ScrollBar();
scrolls[i].Focusable = true;
scrolls[i].Orientation = Orientation.Vertical;
scrolls[i].Minimum = 0;
scrolls[i].Maximum = 255;
scrolls[i].SmallChange = 1;
scrolls[i].LargeChange = 16;
scrolls[i].ValueChanged += ScrollOnValueChanged;
grid.Children.Add(scrolls[i]);
Grid.SetRow(scrolls[i], 1);
Grid.SetColumn(scrolls[i], i); txtValue[i] = new TextBlock();
txtValue[i].TextAlignment = TextAlignment.Center;
txtValue[i].HorizontalAlignment = HorizontalAlignment.Center;
txtValue[i].Margin = new Thickness(5);
grid.Children.Add(txtValue[i]);
Grid.SetRow(txtValue[i], 2);
Grid.SetColumn(txtValue[i], i);
} // Initialize scroll bars.
Color clr = (pnlColor.Background as SolidColorBrush).Color;
scrolls[0].Value = clr.R;
scrolls[1].Value = clr.G;
scrolls[2].Value = clr.B; // Set initial focus.
scrolls[0].Focus();
}
void ScrollOnValueChanged(object sender, RoutedEventArgs args)
{
ScrollBar scroll = sender as ScrollBar;
Panel pnl = scroll.Parent as Panel;
TextBlock txt = pnl.Children[1 +
pnl.Children.IndexOf(scroll)] as TextBlock; txt.Text = String.Format("{0}\n0x{0:X2}", (int)scroll.Value);
pnlColor.Background =
new SolidColorBrush(
Color.FromRgb((byte) scrolls[0].Value,
(byte) scrolls[1].Value,(byte) scrolls[2].Value));
}
}
《WPF程序设计指南》读书笔记——第6章 Dock与Grid的更多相关文章
- css权威指南读书笔记-第10章浮动和定位
这一章看了之后真是豁然开朗,之前虽然写了圣杯布局和双飞翼布局,有些地方也是模糊的,现在打算总结之后再写一遍. 以下都是从<css权威指南>中摘抄的我认为很有用的说明. 浮动元素 一个元素浮 ...
- 《Javascript高级程序设计》读书笔记(1-3章)
第一章 JavaScript简介 1.1 JavaScript简史 略 1.2 JavaScript实现 虽然 JavaScript 和 ECMAScript 通常都被人们用来表达相同的含义,但 Ja ...
- JavaScript权威指南读书笔记【第一章】
第一章 JavaScript概述 前端三大技能: HTML: 描述网页内容 CSS: 描述网页样式 JavaScript: 描述网页行为 特点:动态.弱类型.适合面向对象和函数式编程的风格 语法源自J ...
- 《JavaScript高级程序设计》 - 读书笔记 - 第5章 引用类型
5.1 Object 类型 对象是引用类型的实例.引用类型是一种数据结构,用于将数据和功能组织在一起. 新对象是使用new操作符后跟一个构造函数来创建的.构造函数本身就是一个函数,只不过该函数是出于创 ...
- 《JavaScript高级程序设计》 - 读书笔记 - 第4章 变量、作用域和内存问题
4.1 基本类型和引用类型的值 JavaScript变量是松散类型的,它只是保存特定值的一个名字而已. ECMAScript变量包含两种数据类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据 ...
- 《Linux程序设计》--读书笔记---第十三章进程间通信:管道
管道:进程可以通过它交换更有用的数据. 我们通常是把一个进程的输出通过管道连接到另一个进程的输入: 对shell命令来说,命令的连接是通过管道字符来完成的: cmd1 | cmd2 sh ...
- 《Visual C++ 程序设计》读书笔记 ----第8章 指针和引用
1.&取地址:*取内容. 2.指针变量“++”“--”,并不是指针变量的值加1或减1,而是使指针变量指向下一个或者上一个元素. 3.指针运算符*与&的优先级相同,左结合:++,--,* ...
- 《Linux内核设计与实现》第八周读书笔记——第四章 进程调度
<Linux内核设计与实现>第八周读书笔记——第四章 进程调度 第4章 进程调度35 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配 ...
- 《Linux内核设计与分析》第六周读书笔记——第三章
<Linux内核设计与实现>第六周读书笔记——第三章 20135301张忻估算学习时间:共2.5小时读书:2.0代码:0作业:0博客:0.5实际学习时间:共3.0小时读书:2.0代码:0作 ...
随机推荐
- poj2299解题报告(归并排序求逆序数)
POJ 2299,题目链接http://poj.org/problem?id=2299 题意: 给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列. 思路: 其实就 ...
- 笔记——Function类型 及其 call、apply方法
每个函数都是Function类型的实例.函数有三种定义方式和两个内部属性arguments和this. 同时函数也是对象,也有属性和方法.本篇主要其call()和apply()方法 属性 length ...
- 纯CSS制作“跳动的心”demo
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- overflow:hidden清除浮动原理解析及清除浮动常用方法总结
最近在看<CSS Mastery>这本书,里面有用overflow:hidden来清理浮动的方法.但是一直想不明白为什么能够实现清除浮动,查阅了网络上的解释,下面来总结一下. 一.首先来想 ...
- Ajax中解析Json的两种方法详解
eval(); //此方法不推荐 JSON.parse(); //推荐方法 一.两种方法的区别 我们先初始化一个json格式的对象: var jsonDate = '{ "name&qu ...
- Linux 命令 - su: 以其他用户和组 ID 的身份来运行 shell
在 shell 会话状态下,使用 su 命令将允许你假定为另一个用户的身份,既可以以这个用户的 ID 来启动一个新的 shell 会话,也可以以这个用户的身份来发布一个命令. 命令格式 su [OPT ...
- 每天一道LeetCode--342. Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example:Giv ...
- 软件包 java.util 的分层结构
概述 软件包 类 使用 树 已过时 索引 帮助 JavaTM Platform Standard Ed. 6 上一个 下一个 框架 无框架 所有类 ...
- T-SQL 使用链接库向mysql导数据遇到的奇葩事件一
mysql表结构有 主键 非自增 text longtext类型字段多个 步骤 1.在T-SQL 临时表中处理好所有需要的字段 2.执行openquery语句 字段顺序完全按照mysql字段顺序插入 ...
- python之平台独立的调试工具winpdb介绍
Winpdb is a platform independent graphical GPL Python debugger with support for remote debugging ove ...