浅析Silverlight中ViewBox组件

开发 后端
这里我们将介绍Silverlight中ViewBox组件,本文将为大家介绍该组件的基本特性以及应用实例。

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

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

组件所在命名空间:

System.Windows.Controls

组件常用属性:

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

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

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

实例:

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

MainPage.xaml文件代码:

  1. <UserControl 
  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. <Grid x:Name="LayoutRoot" Width="320" Height="240" Background="White"> 
  8. <Slider x:Name="HSlider" Minimum="0" Maximum="100"  Height="24" Margin="79,0,91,42" VerticalAlignment="Bottom" Width="150"/> 
  9. <Slider x:Name="VSlider" Minimum="0" Maximum="100" HorizontalAlignment="Right" Margin="0,24,57,66" Width="30" Orientation="Vertical" Height="150"/> 
  10. <Border Margin="79,24,91,66" BorderBrush="Black" BorderThickness="1"> 
  11. <Grid x:Name="theContainer" Background="AntiqueWhite"> 
  12. <controlsToolkit:Viewbox x:Name="sampleViewBox" Margin="0,0,-2,-2"> 
  13. <!--放入ViewBox中的按钮对象--> 
  14. <Button Width="101" Content="Button"/> 
  15. </controlsToolkit:Viewbox> 
  16. </Grid> 
  17. </Border> 
  18. <ComboBox x:Name="cbStretch" Height="21" HorizontalAlignment="Left" Margin="8,0,0,8" VerticalAlignment="Bottom" Width="139"/> 
  19. <ComboBox x:Name="cbStretchDirection" Height="21" HorizontalAlignment="Right" Margin="0,0,8,8" VerticalAlignment="Bottom" Width="139"/> 
  20. <TextBlock Height="16" HorizontalAlignment="Left" Margin="9,0,0,33" VerticalAlignment="Bottom" Width="66" Text="拉伸模式:" TextWrapping="Wrap"/> 
  21. <TextBlock Height="16" HorizontalAlignment="Right" Margin="0,0,8,33" VerticalAlignment="Bottom" Width="56" Text="拉伸方向:" TextWrapping="Wrap"/> 
  22. </Grid> 
  23. </UserControl> 

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<StretchHelper> cbStretchList = new List<StretchHelper>();  
  30. List<StretchDirectionHelper> cbStretchDirectionList = new List<StretchDirectionHelper>();  
  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<double>(HSlider_ValueChanged);  
  39. this.VSlider.ValueChanged += new RoutedPropertyChangedEventHandler<double>(VSlider_ValueChanged);  
  40. }  
  41. void VSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)  
  42. {  
  43. sampleViewBox.Height = theContainer.ActualHeight * VSlider.Value / 100.0;  
  44. }  
  45. void HSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> 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个新功能
责任编辑:彭凡 来源: 博客园
相关推荐

2009-11-26 13:12:16

Silverlight

2009-04-03 13:09:12

Windows Emb

2011-12-29 15:35:39

Web

2011-12-30 09:49:36

Silverlight

2009-11-17 10:47:14

Silverlight

2009-09-28 10:35:45

Silverlight

2009-09-27 13:38:03

Silverlight

2009-12-29 16:36:47

Silverlight

2009-12-31 17:31:23

Silverlight

2009-12-31 16:50:02

Silverlight

2009-09-22 18:39:02

Silverlight

2009-07-15 11:02:32

Swing组件

2009-09-17 16:41:12

C#组件编程

2009-08-05 16:53:14

ASP.NET组件设计

2009-10-14 10:26:00

Route组件

2009-08-10 16:07:44

ASP.NET Lin

2009-08-10 15:42:33

ASP.NET Che

2009-11-13 10:10:07

2022-05-10 07:46:08

Envoy网络通讯

2024-01-09 09:06:13

点赞
收藏

51CTO技术栈公众号