浅析Silverlight中ViewBox组件
创始人
2024-06-07 18:50:16
0

这里我们将介绍Silverlight中ViewBox组件,这个组件的作用主要是做布局与视觉效果。并给出实例代码和最终效果图。

ViewBox组件的作用是拉伸或延展位于其中的组件,使之有更好的布局及视觉效果。本文将为大家介绍该组件的基本特性以及应用实例。

组件所在命名空间:

System.Windows.Controls

组件常用属性:

Child:获取或设置一个ViewBox元素的单一子元素。

Stretch:获取或设置拉伸模式以决定该组件中的内容以怎样的形式填充该组件的已有空间。

StretchDirection:获取或设置该组件的拉伸方向以决定该组件中的内容将以何种形式被延展。

实例:

详细的说明在代码注释中给出。

MainPage.xaml文件代码:

  1.  
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
  5. mc:Ignorable="d" xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" x:Class="SilverlightClient.MainPage" 
  6. d:DesignWidth="320" d:DesignHeight="240"> 
  7.  x:Name="LayoutRoot" Width="320" Height="240" Background="White"> 
  8.  x:Name="HSlider" Minimum="0" Maximum="100"  Height="24" Margin="79,0,91,42" VerticalAlignment="Bottom" Width="150"/> 
  9.  x:Name="VSlider" Minimum="0" Maximum="100" HorizontalAlignment="Right" Margin="0,24,57,66" Width="30" Orientation="Vertical" Height="150"/> 
  10.  Margin="79,24,91,66" BorderBrush="Black" BorderThickness="1"> 
  11.  x:Name="theContainer" Background="AntiqueWhite"> 
  12.  x:Name="sampleViewBox" Margin="0,0,-2,-2"> 
  13.  
  14.  Width="101" Content="Button"/> 
  15.  
  16.  
  17.  
  18.  x:Name="cbStretch" Height="21" HorizontalAlignment="Left" Margin="8,0,0,8" VerticalAlignment="Bottom" Width="139"/> 
  19.  x:Name="cbStretchDirection" Height="21" HorizontalAlignment="Right" Margin="0,0,8,8" VerticalAlignment="Bottom" Width="139"/> 
  20.  Height="16" HorizontalAlignment="Left" Margin="9,0,0,33" VerticalAlignment="Bottom" Width="66" Text="拉伸模式:" TextWrapping="Wrap"/> 
  21.  Height="16" HorizontalAlignment="Right" Margin="0,0,8,33" VerticalAlignment="Bottom" Width="56" Text="拉伸方向:" TextWrapping="Wrap"/> 
  22.  
  23.  

MainPage.xaml.cs文件代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Documents;  
  8. using System.Windows.Input;  
  9. using System.Windows.Media;  
  10. using System.Windows.Media.Animation;  
  11. using System.Windows.Shapes;  
  12. namespace SilverlightClient  
  13. {  
  14. //辅助类StretchHelper  
  15. public class StretchHelper  
  16. {  
  17. public string StretchModeName { get; set; }  
  18. public Stretch theStretchMode { get; set; }  
  19. }  
  20. //辅助类StretchDirectionHelper  
  21. public class StretchDirectionHelper  
  22. {  
  23. public string StretchDirectionName { get; set; }  
  24. public StretchDirection theStretchDirection { get; set; }  
  25. }  
  26. public partial class MainPage : UserControl  
  27. {  
  28. //定义cbStretch与cbStretchDirection的数据源  
  29. List cbStretchList = new List();  
  30. List cbStretchDirectionList = new List();  
  31. public MainPage()  
  32. {  
  33. InitializeComponent();  
  34. //注册事件触发  
  35. this.Loaded += new RoutedEventHandler(MainPage_Loaded);  
  36. this.cbStretch.SelectionChanged += new SelectionChangedEventHandler(cbStretch_SelectionChanged);  
  37. this.cbStretchDirection.SelectionChanged += new SelectionChangedEventHandler(cbStretchDirection_SelectionChanged);  
  38. this.HSlider.ValueChanged += new RoutedPropertyChangedEventHandler(HSlider_ValueChanged);  
  39. this.VSlider.ValueChanged += new RoutedPropertyChangedEventHandler(VSlider_ValueChanged);  
  40. }  
  41. void VSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)  
  42. {  
  43. sampleViewBox.Height = theContainer.ActualHeight * VSlider.Value / 100.0;  
  44. }  
  45. void HSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)  
  46. {  
  47. sampleViewBox.Width = theContainer.ActualWidth * HSlider.Value / 100.0;  
  48. }  
  49. void cbStretchDirection_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  50. {  
  51. if (cbStretchDirection.SelectedItem != null)  
  52. {  
  53. sampleViewBox.StretchDirection = (cbStretchDirection.SelectedItem as StretchDirectionHelper).theStretchDirection;  
  54. }  
  55. }  
  56. void cbStretch_SelectionChanged(object sender, SelectionChangedEventArgs e)  
  57. {  
  58. if (cbStretch.SelectedItem != null)  
  59. {  
  60. sampleViewBox.Stretch = (cbStretch.SelectedItem as StretchHelper).theStretchMode;  
  61. }  
  62. }  
  63. void MainPage_Loaded(object sender, RoutedEventArgs e)  
  64. {  
  65. //填充各ComboBox内容  
  66. cbStretchList.Add(new StretchHelper() { StretchModeName = "Fill", theStretchMode = Stretch.Fill });  
  67. cbStretchList.Add(new StretchHelper() { StretchModeName = "None", theStretchMode = Stretch.None });  
  68. cbStretchList.Add(new StretchHelper() { StretchModeName = "Uniform", theStretchMode = Stretch.Uniform });  
  69. cbStretchList.Add(new StretchHelper() { StretchModeName = "UniformToFill", theStretchMode = Stretch.UniformToFill });  
  70. cbStretch.ItemsSource = cbStretchList;  
  71. cbStretch.DisplayMemberPath = "StretchModeName";  
  72. cbStretchDirectionList.Add(new StretchDirectionHelper() { StretchDirectionName = "DownOnly", theStretchDirection = StretchDirection.DownOnly });  
  73. cbStretchDirectionList.Add(new StretchDirectionHelper() { StretchDirectionName = "UpOnly", theStretchDirection = StretchDirection.UpOnly });  
  74. cbStretchDirectionList.Add(new StretchDirectionHelper() { StretchDirectionName = "Both", theStretchDirection = StretchDirection.Both });  
  75. cbStretchDirection.ItemsSource = cbStretchDirectionList;  
  76. cbStretchDirection.DisplayMemberPath = "StretchDirectionName";  
  77. }  
  78. }  

最终效果图:

最终效果图 

本文来自Kinglee博客园文章《有关ViewBox组件的研究——Silverlight学习笔记[34]

【编辑推荐】

  1. Office 2010将使用Silverlight改善用户体验
  2. 微软.NET平台主管谈Silverlight企业级开发
  3. Flash与Silverlight多领域实测对比
  4. 微软宣称Silverlight装机量超过三亿
  5. 图解Silverlight 3的7个新功能

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...