windows phone (18) Border元素

简介:

 Border类是对某一个对象的周围边框,背景,或者同时绘制两者,首先看一个简单的例子进行分析【作者:神舟龍

xaml文件:

 
  1. <!--ContentPanel - 在此处放置其他内容--> 
  2.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"
  3.             <Border Background="DarkCyan" BorderBrush="Coral"  BorderThickness="20"  CornerRadius="19"
  4.                 <TextBlock x:Name="tbShow" Text="边框元素示例" VerticalAlignment="Center" HorizontalAlignment="Center"
  5.                     <TextBlock.RenderTransform> 
  6.                         <CompositeTransform Rotation="30"></CompositeTransform> 
  7.                     </TextBlock.RenderTransform> 
  8.                 </TextBlock> 
  9.             </Border> 
  10.         </Grid> 

上面代码中BorderBrush表示边框颜色,它是Brush类型的,所以可以设置渐变画刷;BorderThickness表示边框的粗细,它 是Thickness类型的,Thickness是用于Margin和Padding的结构体,所以可以分别为上下左右设置不同个宽 度;CornerRadius表示设置边框角的半径,它是CornerRadius结构体,所以运行为四个角设置不同的圆角半径值;可以看到 TextBlock直接镶嵌在Border中,这是因为Border有个属性是Child,因为Child属性是Border的 ContentProperty属性,所以Border.Child标记不是必须的,实现的效果:

 

 需 要注意的是Child属性只能设置一个UIElement类型的元素,所以我们可以在里面放些textblock,image等,如果要在border里 进行元素扩展,可以使用面板stackpanel,canvas,grid,里面嵌套其他元素;从上图可以看出,我们定义的border是整个填充 grid的,这是因为border元素的水平位置HorizontalAlignment和垂直位置VerticalAlignment默认值为Stretch,所以会拉伸填充整个父元素,所以一般会设置border的宽和高;


下面的示例在隐藏文件cs实现的四个角的圆角半径不同,边框的每个边的粗细不同,并绘制边框颜色xaml文件代码:

 
  1. <!--ContentPanel - 在此处放置其他内容--> 
  2.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"
  3.             <Border x:Name="bd" Background="DarkCyan" ManipulationDelta="bd_ManipulationDelta"
  4.                 <TextBlock x:Name="tbShow" Text="边框元素示例" VerticalAlignment="Center" HorizontalAlignment="Center"
  5.                     <TextBlock.RenderTransform> 
  6.                         <CompositeTransform Rotation="30"></CompositeTransform> 
  7.                     </TextBlock.RenderTransform> 
  8.                 </TextBlock> 
  9.             </Border> 
  10.         </Grid> 

 隐藏文件代码:

 
  1. // <summary> 
  2.         /// 触摸移动实现 
  3.         /// </summary> 
  4.         /// <param name="sender"></param> 
  5.         /// <param name="e"></param> 
  6.         private void bd_ManipulationDelta(object sender, ManipulationDeltaEventArgs e) 
  7.         { 
  8.             //左上右下 
  9.             Thickness thickNess = new Thickness(10, 20, 30, 40); 
  10.             //设置不同的粗细 
  11.             bd.BorderThickness = thickNess
  12.             //设置左上,右上,左下,右下的圆角半径值 
  13.             CornerRadius cornerRadius = new CornerRadius(10, 20, 30, 40); 
  14.             bd.CornerRadius = cornerRadius
  15.  
  16.             LinearGradientBrush lgb = new LinearGradientBrush(); 
  17.             GradientStopCollection gsc = new GradientStopCollection(); 
  18.             GradientStop gs1 = new GradientStop(); 
  19.             gs1.Color = Color.FromArgb(122, 102, 213, 167); 
  20.             gs1.Offset = 0.2; 
  21.             gsc.Add(gs1); 
  22.             GradientStop gs2 = new GradientStop(); 
  23.             gs2.Color = Color.FromArgb(102, 102, 133, 167); 
  24.             gs2.Offset = 0.7; 
  25.             gsc.Add(gs2); 
  26.             lgb.GradientStops = gsc
  27.             //实现边框颜色 
  28.             bd.BorderBrush = lgb
  29.         } 
  30. 复制代码 

效果:

 



本文转自shenzhoulong  51CTO博客,原文链接:http://blog.51cto.com/shenzhoulong/841791,如需转载请自行联系原作者

 

相关文章
|
Android开发 iOS开发 Windows
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
不久前,随着最后一家WP手机厂商惠普宣布取消今后Windows Phone的研发计划,以及微软官方声明对WP8.1系统今后所有升级维护的终止,WP手机,作为曾经和安卓手机、苹果手机并驾齐驱的三大智能手机之一,正式寿终正寝。
1250 0
Windows Phone 寿终正寝了,这些经典机型你还记得吗?
|
XML 开发框架 前端开发
Windows Phone快速入门需掌握哪些能力
在此之前,先普及下Windows Phone的概念和开发工具的介绍。 Windows Phone是微软公司开发的手机操作系统,它将微软旗下的Xbox Live游戏、Xbox Music音乐与独特的视频体验集成至手机中。2012年6月21日,微软正式发布Windows Phone 8,采用和Windows 8相同的Windows NT内核,同时也针对市场的Windows Phone 7.5发布Windows Phone 7.8。
133 0
Windows Phone快速入门需掌握哪些能力
|
编解码 前端开发 JavaScript
Windows Phone 下开发 LBS 应用
基于位置的服务(Location Based Service,LBS),它是通过电信移动运营商的无线电通讯网络(如GSM网、CDMA网)或外部定位方式(如GPS)获取移动终端用户的位置信息(地理坐标,或大地坐标),在GIS(Geographic Information System,地理信息系统)平台的支持下,为用户提供相应服务的一种增值业务。
162 0
|
移动开发 Android开发 开发者
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
在Build 2014大会上,微软正式发布了传闻已久的Windows Phone 8.1系统,所有的Windows Phone 8手机都可以升级,微软这次可谓是十分厚道。虽然并非迭代升级,但WP 8.1还是拥有很多重大更新,对于微软进一步完善移动平台拥有积极的意义。下面,就一起来了解一下WP 8.1的主要新特性。
230 0
Windows Phone 8.1 新功能汇总 开发者预览版开放下载
|
Windows
背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素
原文:背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素 [源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control -...
1110 0