WPF之路——ViewBox组件

http://msdn.microsoft.com/zh-cn/library/system.windows.controls.viewbox.aspx

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

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

组件所在命名空间:

System.Windows.Controls

组件常用属性:

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

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

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

实例:

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

MainPage.xaml文件代码:


    
        
        
        
            
                
                    
                    



MainPage.xaml.cs文件代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    //辅助类StretchHelper  
    public class StretchHelper
    {
        public string StretchModeName { get; set; }
        public Stretch theStretchMode { get; set; }
    }
    //辅助类StretchDirectionHelper  
    public class StretchDirectionHelper
    {
        public string StretchDirectionName { get; set; }
        public StretchDirection theStretchDirection { get; set; }
    }  

    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        //定义cbStretch与cbStretchDirection的数据源  
        List cbStretchList = new List();
        List cbStretchDirectionList = new List();  
      
        public MainWindow()
        {
            InitializeComponent();
            //注册事件触发  
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            this.cbStretch.SelectionChanged += new SelectionChangedEventHandler(cbStretch_SelectionChanged);
            this.cbStretchDirection.SelectionChanged += new SelectionChangedEventHandler(cbStretchDirection_SelectionChanged);
            this.HSlider.ValueChanged += new RoutedPropertyChangedEventHandler(HSlider_ValueChanged);
            this.VSlider.ValueChanged += new RoutedPropertyChangedEventHandler(VSlider_ValueChanged);
        }
        void VSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            sampleViewBox.Height = theContainer.ActualHeight * VSlider.Value / 100.0;
        }
        void HSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
        {
            sampleViewBox.Width = theContainer.ActualWidth * HSlider.Value / 100.0;
        }
        void cbStretchDirection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbStretchDirection.SelectedItem != null)
            {
                sampleViewBox.StretchDirection = (cbStretchDirection.SelectedItem as StretchDirectionHelper).theStretchDirection;
            }
        }
        void cbStretch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbStretch.SelectedItem != null)
            {
                sampleViewBox.Stretch = (cbStretch.SelectedItem as StretchHelper).theStretchMode;
            }
        }
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            //填充各ComboBox内容  
            cbStretchList.Add(new StretchHelper() { StretchModeName = "Fill", theStretchMode = Stretch.Fill });
            cbStretchList.Add(new StretchHelper() { StretchModeName = "None", theStretchMode = Stretch.None });
            cbStretchList.Add(new StretchHelper() { StretchModeName = "Uniform", theStretchMode = Stretch.Uniform });
            cbStretchList.Add(new StretchHelper() { StretchModeName = "UniformToFill", theStretchMode = Stretch.UniformToFill });
            cbStretch.ItemsSource = cbStretchList;
            cbStretch.DisplayMemberPath = "StretchModeName";
            cbStretchDirectionList.Add(new StretchDirectionHelper() { StretchDirectionName = "DownOnly", theStretchDirection = StretchDirection.DownOnly });
            cbStretchDirectionList.Add(new StretchDirectionHelper() { StretchDirectionName = "UpOnly", theStretchDirection = StretchDirection.UpOnly });
            cbStretchDirectionList.Add(new StretchDirectionHelper() { StretchDirectionName = "Both", theStretchDirection = StretchDirection.Both });
            cbStretchDirection.ItemsSource = cbStretchDirectionList;
            cbStretchDirection.DisplayMemberPath = "StretchDirectionName";
        }  
    }
}



WPF之路——ViewBox组件_第1张图片

你可能感兴趣的:(WPF笔记)