浅析Silverlight中ViewBox组件

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

创新互联是一家以网络技术公司,为中小企业提供网站维护、成都做网站、成都网站设计、网站备案、服务器租用、域名注册、软件开发、微信小程序开发等企业互联网相关业务,是一家有着丰富的互联网运营推广经验的科技公司,有着多年的网站建站经验,致力于帮助中小企业在互联网让打出自已的品牌和口碑,让企业在互联网上打开一个面向全国乃至全球的业务窗口:建站来电联系:18980820575

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个新功能

当前标题:浅析Silverlight中ViewBox组件
文章源于:http://www.gawzjz.com/qtweb2/news17/18317.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联