《WPF程序设计指南》读书笔记——第5章 Stack与Wrap
1.StackPanel面板
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls; namespace LY.StactTheButtons
{
public class StackTheButton:Window
{
[STAThread]
public static void Main()
{
new Application().Run(new StackTheButton());
}
public StackTheButton()
{
Title = "Stack The Button";
StackPanel stack = new StackPanel();
stack.Background = Brushes.AliceBlue;
//设定面板的子元素水平方向排列
//stack.Orientation = Orientation.Horizontal;
//设定面板对齐方式为在窗口中央
//stack.HorizontalAlignment = HorizontalAlignment.Center;
//将窗体根据内容设定大小
SizeToContent = SizeToContent.WidthAndHeight;
ResizeMode = ResizeMode.CanMinimize;
stack.Margin = new Thickness(5);
Content = stack;
Random rand = new Random();
for (int i = 0; i < 10; i++)
{
Button btn = new Button();
btn.Name = "A" + i;
//FontSize默认值为System.Windows.SystemFonts.MessageFontSize
btn.FontSize += rand.Next(10);
//btn.HorizontalAlignment = HorizontalAlignment.Center;
btn.Margin = new Thickness(5);
btn.Content = "Button " + btn.Name + " Says 'Click Me'";
stack.Children.Add(btn);
//btn.Click += btn_Click;
}
//添加事件,可取代上面的btn.Click+=btn_Click,但两者的区别是sender对象不同
//AddHandler定义的事件会对面板中的所以button对象起作用
//这里如果去掉stack也可以,即设定为窗口为sender
stack.AddHandler(Button.ClickEvent, new RoutedEventHandler(btn_Click));
stack.Children[0].Focus();
}
void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = e.Source as Button;
MessageBox.Show("Button " + btn.Name + " has been clicked", "Button Click");
}
}
}
Stack面板嵌套/ScrollViewer控件/Viewbox控件/e.Source的使用注意
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace LY.StackThirtyButtons
{
class StackThirtyButtons:Window
{
[STAThread]
public static void Main()
{
new Application().Run(new StackThirtyButtons());
}
public StackThirtyButtons()
{
Title = "嵌套Stack面板";
AddHandler(Button.ClickEvent, new RoutedEventHandler(Button_Click)); //使用可滚动区域控件
ScrollViewer scroll = new ScrollViewer();
scroll.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
scroll.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
Content = scroll;
//Viewbox控件可以代替ScrollViewer使用,它没有滚动条,
//但会根据内容缩放其大小,保证所有内容可见
//Viewbox view = new Viewbox();
//Content = view; StackPanel mainStack = new StackPanel();
mainStack.Orientation = Orientation.Horizontal;
mainStack.Margin = new Thickness(5);
scroll.Content = mainStack;
//view.Child = mainStack;
for (int i = 0; i < 3; i++)
{
StackPanel stack = new StackPanel();
stack.Margin = new Thickness(5);
mainStack.Children.Add(stack);
for (int j = 0; j < 10; j++)
{
Button btn = new Button();
btn.Content = "Button No. " + (10 * i + j + 1);
stack.Children.Add(btn);
}
}
} private void Button_Click(object sender, RoutedEventArgs e)
{
//e.Source指向Button,而此时Sender是this
Button btn = e.Source as Button;
/*
* 因为AddHandler的Sender是窗口,当点击滚动条上的
* 按钮(RepeatButton类型,继承自ButtonBase,有Click事件),
* 则无法转换成Button,btn此时为Null,可以在
* AddHandler前加上mainStack,将其Sender设定为mainStack
*/
if (btn != null)
MessageBox.Show(btn.Content + " has been clicked", "Button Click");
}
}
}
GroupBox控件/RadioButton控件
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls; namespace LY.TuneTheRadio
{
class TuneTheRadio : Window
{
[STAThread]
public static void Main()
{
new Application().Run(new TuneTheRadio());
}
public TuneTheRadio()
{
Title = "单选按钮的使用";
SizeToContent = SizeToContent.WidthAndHeight;
GroupBox group = new GroupBox();
//设定GroupBox控件的标头属性
group.Header = "Window Style属性";
group.Margin = new Thickness(96);
group.Padding = new Thickness(5);
Content = group;
StackPanel stack = new StackPanel();
group.Content = stack;
stack.Children.Add(CreateRadioButton("无边框样式", WindowStyle.None, "1"));
stack.Children.Add(CreateRadioButton("单边框样式", WindowStyle.SingleBorderWindow, "1"));
stack.Children.Add(CreateRadioButton("三维边框样式", WindowStyle.ThreeDBorderWindow, "2"));
stack.Children.Add(CreateRadioButton("固定工具框样式", WindowStyle.ToolWindow, "2"));
AddHandler(RadioButton.CheckedEvent, new RoutedEventHandler(RadioOnChecked));
} private void RadioOnChecked(object sender, RoutedEventArgs e)
{
RadioButton radio = e.Source as RadioButton;
WindowStyle = (WindowStyle)radio.Tag;
} private RadioButton CreateRadioButton(string strText, WindowStyle winStyle, string groupNum)
{
RadioButton radio = new RadioButton();
//GroupName属性可以将GroupBox中的单选按钮分成组
radio.GroupName = groupNum;
radio.Content = strText;
//存储有关此元素的自定义信息
radio.Tag = winStyle;
radio.Margin = new Thickness(5);
radio.IsChecked = (WindowStyle == winStyle);
return radio;
}
}
}
将GroupBox的Content设定为StackPanel,再在StackPanel的Children中加入多个RadioButton。也就是说只有面板Panel中才能放多个控件对象,其他ContentControl只能通过Content放入一个对象。主要的面板控件有:

2.WrapPanel面板
WrapPanel以由左到右,由上到下形式摆放控件;一般用于不确定包含多少个控件时,如资源管理器。平常用StackPanel比较多。
WrapPanel面板一般搭配ScrollViewer控件使用。
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls; namespace LY.ExploreDirectories
{
class ExploreDirectories : Window
{
[STAThread]
public static void Main()
{
new Application().Run(new ExploreDirectories());
}
public ExploreDirectories()
{
Title = "Explore Directories";
ScrollViewer scroll = new ScrollViewer();
Content = scroll;
WrapPanel wrap = new WrapPanel();
scroll.Content = wrap;
wrap.Children.Add(new FileSystemInfoButton());
}
}
}
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls;
using System.IO;
using System.Diagnostics; namespace LY.ExploreDirectories
{
public class FileSystemInfoButton : Button
{
FileSystemInfo info;
public FileSystemInfoButton()
: this(new DirectoryInfo(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)))
{
}
public FileSystemInfoButton(FileSystemInfo info)
{
this.info = info;
Content = info.Name;
if (info is DirectoryInfo)
FontWeight = FontWeights.Bold;
Margin = new Thickness(10);
}
public FileSystemInfoButton(FileSystemInfo info, string str)
: this(info)
{
Content = str;
}
protected override void OnClick()
{
if (info is FileInfo)
Process.Start(info.FullName);
else if (info is DirectoryInfo)
{
DirectoryInfo dir = info as DirectoryInfo;
Application.Current.MainWindow.Title = dir.FullName;
Panel pnl = Parent as Panel;
pnl.Children.Clear();
if (dir.Parent != null)
pnl.Children.Add(new FileSystemInfoButton(dir.Parent, ".."));
foreach (FileSystemInfo inf in dir.GetFileSystemInfos())
{
pnl.Children.Add(new FileSystemInfoButton(inf));
}
}
base.OnClick();
}
}
}
《WPF程序设计指南》读书笔记——第5章 Stack与Wrap的更多相关文章
- 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作 ...
随机推荐
- 转摘:常用ubuntu 关机,重启,注销命令
1.关机命令 shutdown 好像ubuntu的终端中默认的是当前用户的命令,只是普通用户,因此在终端器中可以使用sudo -sh 转换到管理员root用户下执行命令. 1).shutdown –h ...
- Visual studio 2012 添加 GitHub
文件-新建项目
- 奥运会订票系统c语言代写源码下载
制作能够实现2008北京奥运会网上订票的系统,能够实现购票人员注册.购票.管理人员可以设置各个比赛场地的赛事安排及票数. 程序要求实现的功能如下: 购票者信息注册:购票者可以用昵称和身份证进行注册,若 ...
- 【记忆化搜索】Codeforces Round #295 (Div. 2) B - Two Buttons
题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m: 随后发篇随笔普及下memset函数的初始化问题.自己也是涨了好多姿势. 代码 #include<iostream> ...
- Android开发需要注意的坑
Android开发需要注意的坑一览对于一些Android开发过程中坑爹.细小,但又重要的错误的总结Android开发在路上:少去踩坑,多走捷径其他参考: google官方版本发布图 umeng ...
- Mirco2440核心板设计思考
1.核心板架构 注意的是:此处的RAM和ROM都是外置的 硬件框架 S3C2440+ SDRAM + NAND Flash + NOR Flash 也就是 CPU + RAM + ROM 2.S3C2 ...
- overflow:hidden清除浮动原理解析及清除浮动常用方法总结
最近在看<CSS Mastery>这本书,里面有用overflow:hidden来清理浮动的方法.但是一直想不明白为什么能够实现清除浮动,查阅了网络上的解释,下面来总结一下. 一.首先来想 ...
- 重叠I/O之可等待的重叠I/O【系列一】
一 什么是异步I/O 同步I/O和异步I/O的关键不同就是在发出I/O请求后,线程是否会阻塞.当线程发出一个设备I/O请求的时候,线程会被挂起来,直到设备完成I/O请求为止,这称之为同步I/O.而对于 ...
- ###《VIM实用技巧》
###<VIM实用技巧> #@author: gr #@date: 2015-11-20 #@email: forgerui@gmail.com <VIM实用技巧>阅读笔记. ...
- Quartz2D 图像处理
首先感谢一片枫叶总结出这么好的文章,文章出处:http://www.cnblogs.com/smileEvday/archive/2013/05/25/IOSImageEdit.html 本文将为大家 ...