Applications=Code+Markup 读书笔记之三 内容的概念
三 内容的概念
Content property Object类型,但不能设定成另一个Window类型的对象。
content 分为两组对象:一组继承自UIElement(称为element),利用OnRender显示;另一组用 ToString方法显示。
1 Font 在WPF中没有Font类
一般 em size(常用的字型测量法)是以“点”为单位的,即1/72英寸,
在WPF中 FontSize使用“与设备无关的单位”,即1/96英寸。
默认的FontSize property 是11,也就是11*3/4 = 8.25点
2 窗口大小的改变
SizeToContent = SizeToContent.WidthAndHeight;
书上说:窗口会根据内容的宽和高来作调整。我测试了一下,窗口里放了一个Button,发现窗口大小主要和Button的Margin有关,而且在设计界面做好的布局,运行之后发现控件位置很乱。慎用。
ResizeMode = ResizeMode.CanMinimize;
去掉最大化按钮,且无法调整窗口大小。
ResizeMode = ResizeMode.NoResize;
去掉最大化和最小化按钮,且无法调整窗口大小。
3 UIElement类 实现键盘、鼠标、手写笔事件处理,唯一直接继承UIElement的类是FrameworkElement。
4 Image 可以在System.Windows.Controls命名空间下找到,但非继承自Controls 而是继承自FrameworkElement。
BitmapImage类位于System.Windows.Media.Image命名空间下。
示例:
Uri uri = new Uri(System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"),"XXX.bmp"));
BitmapImage bitmap = new BitmapImage(uri);
Image img = new Image();
img.Source = bitmap;
img.Opacity = 0.5; // 设置透明度(0-1),可以看到Background 渐变色
Background = new LinearGradientBrush(Colors.Red,Colors.Blue,new Point(0,0),new Point(1,1));
img.LayoutTransform = new RotateTransform(45); // 顺时针旋转45度
img.Stretch = Stretch.None; // 以原始图像尺寸显示
img.Stretch = Stretch.Fill; // 填充整个窗口,一般失真。
img.Stretch = Stretch.UniformToFill; // 均匀拉伸,但会把整个客户区覆盖,超出部分被截掉
Content = img;
注:Environment..::.GetEnvironmentVariable 从当前进程检索环境变量的值,如:Environment.GetEnvironmentVariable("Temp") ,环境变量名不区分大小写。
5 Shape
Object
DispatcherObject(抽象类)
DependencyObject
Visual(抽象类)
UIElement
FrameworkElement
Shape(抽象类)
Ellipse
Line
Path
Polygon
Rectangle
6 ContentElement 与 ContentControl
TextBlock txt = new TextBlock();
txt.FontSize = 32; //24 points
txt.Inlines.Add("XXXXX");
txt.Inlines.Add("YYYYYY");
Content = txt;
Inlines类继承图
Object
DispatcherObject(抽象类)
DependencyObject
ContentElement
FrameWorkContentElemental
TextElement
Inline(抽象类)
Run
Span
Bold
Hyperlink
Italic
Underline
ContentControl是控件 可以具有Content property;ContentElement对象必须是控件的内容才能得以显示。
ContentElement也定义了许多用户输入事件
示例:把事件处理器安装到Inline element上