稳扎稳打Silverlight(5) - 2.0控件之ListBox, MediaElement, MultiScaleImage, PasswordBox

索引页]
[源码下载]


稳扎稳打Silverlight(5) - 2.0控件之ListBox, MediaElement, MultiScaleImage, PasswordBox, ProgressBar, RadioButton


作者: webabcd


介绍
Silverlight 2.0 控件一览:ListBox, MediaElement, MultiScaleImage, PasswordBox, ProgressBar, RadioButton 


在线DEMO
http://webabcd.blog.51cto.com/1787395/342779  


示例 
1、ListBox.xaml
<UserControl x:Class="Silverlight20.Control.ListBox" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <StackPanel HorizontalAlignment="Left"> 
                 
                <!-- 
                SelectionChanged - ListBox中某个对象被选中后所触发的事件 
                --> 
                <ListBox Margin="5" Width="200" Height="100" SelectionChanged="ListBox_SelectionChanged"> 
                        <ListBoxItem Content="ListBoxItem01" /> 
                        <ListBoxItem Content="ListBoxItem02" /> 
                        <ListBoxItem Content="ListBoxItem03" /> 
                        <ListBoxItem Content="ListBoxItem04" /> 
                        <ListBoxItem Content="ListBoxItem05" /> 
                        <ListBoxItem Content="ListBoxItem06" /> 
                        <ListBoxItem Content="ListBoxItem07" /> 
                        <ListBoxItem Content="ListBoxItem08" /> 
                        <ListBoxItem Content="ListBoxItem09" /> 
                        <ListBoxItem Content="ListBoxItem10" /> 
                </ListBox> 
                 
                <!-- 
                ListBox中可以包含任何对象 
                --> 
                <ListBox Margin="5" Width="200"> 
                        <TextBlock Text="TextBlock" /> 
                        <TextBox Text="TextBox" /> 
                        <Button Content="Button" /> 
                </ListBox> 
                 
        </StackPanel> 
</UserControl>
 
ListBox.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif using System.Windows.Browser; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Control 
InBlock.gif
InBlock.gif         public partial  class ListBox : UserControl 
InBlock.gif        { 
InBlock.gif                 public ListBox() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void ListBox_SelectionChanged( object sender, SelectionChangedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         // ListBox.SelectedItem - ListBox中被选中的对象 
InBlock.gif 
InBlock.gif                        var lst = sender  as System.Windows.Controls.ListBox; 
InBlock.gif 
InBlock.gif                        MessageBox.Show( 
InBlock.gif                                ((System.Windows.Controls.ListBoxItem)lst.SelectedItem).Content +  " 被单击了"
InBlock.gif                                 "提示"
InBlock.gif                                MessageBoxButton.OK); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
2、MediaElement.xaml
<UserControl x:Class="Silverlight20.Control.MediaElement" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <StackPanel HorizontalAlignment="Center"> 
                 
                <!-- 
                Source - 视频路径 
                AutoPlay - 是否自动播放 
                --> 
                <MediaElement x:Name="mediaElement" Height="250" AutoPlay="False" 
                                        Source="/Silverlight20;component/Video/Demo.wmv"    /> 
                 
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 
                        <ToggleButton x:Name="play" Content="播放" Margin="5" Click="play_Click" /> 
                        <ToggleButton x:Name="mute" Content="静音" Margin="5" Click="mute_Click" /> 
                </StackPanel> 
        </StackPanel> 
</UserControl>
 
MediaElement.xaml.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Control 
InBlock.gif
InBlock.gif         public partial  class MediaElement : UserControl 
InBlock.gif        { 
InBlock.gif                 public MediaElement() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void play_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        var tb = sender  as System.Windows.Controls.Primitives.ToggleButton; 
InBlock.gif                         if (tb.IsChecked ==  true
InBlock.gif                        { 
InBlock.gif                                tb.Content =  "暂停"
InBlock.gif 
InBlock.gif                                 // MediaElement.Play() - 播放视频 
InBlock.gif                                 this.mediaElement.Play(); 
InBlock.gif                        } 
InBlock.gif                         else 
InBlock.gif                        { 
InBlock.gif                                tb.Content =  "播放"
InBlock.gif 
InBlock.gif                                 // MediaElement.Pause() - 暂停视频 
InBlock.gif                                 // MediaElement.Stop() - 停止视频 
InBlock.gif                                 this.mediaElement.Pause(); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void mute_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        var tb = sender  as System.Windows.Controls.Primitives.ToggleButton; 
InBlock.gif                         if (tb.IsChecked ==  true
InBlock.gif                        { 
InBlock.gif                                tb.Content =  "有声"
InBlock.gif 
InBlock.gif                                 // MediaElement.IsMuted - 是否静音 
InBlock.gif                                 // MediaElement.Volume - 声音大小(0 - 1) 
InBlock.gif                                 this.mediaElement.IsMuted =  true
InBlock.gif                        } 
InBlock.gif                         else 
InBlock.gif                        { 
InBlock.gif                                tb.Content =  "静音"
InBlock.gif                                 this.mediaElement.IsMuted =  false
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
3、MultiScaleImage.xaml
<UserControl x:Class="Silverlight20.Control.MultiScaleImage" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <StackPanel HorizontalAlignment="Left"> 
                 
                <MultiScaleImage x:Name="msi" Width="400" Height="300"></MultiScaleImage> 
                 
        </StackPanel> 
</UserControl>
 
MultiScaleImage.xaml.cs(支持放大/缩小/拖动/滚轮之类的,摘自Deep Zoom Composer生成的代码)
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Net; 
InBlock.gif using System.Windows; 
InBlock.gif using System.Windows.Controls; 
InBlock.gif using System.Windows.Documents; 
InBlock.gif using System.Windows.Input; 
InBlock.gif using System.Windows.Media; 
InBlock.gif using System.Windows.Media.Animation; 
InBlock.gif using System.Windows.Shapes; 
InBlock.gif 
InBlock.gif namespace Silverlight20.Control 
InBlock.gif
InBlock.gif         public partial  class MultiScaleImage : UserControl 
InBlock.gif        { 
InBlock.gif                 // 
InBlock.gif                 // Based on prior work done by Lutz Gerhard, Peter Blois, and Scott Hanselman 
InBlock.gif                 // 
InBlock.gif                Point lastMousePos =  new Point(); 
InBlock.gif 
InBlock.gif                 double _zoom = 1; 
InBlock.gif                 bool mouseButtonPressed =  false
InBlock.gif                 bool mouseIsDragging =  false
InBlock.gif                Point dragOffset; 
InBlock.gif                Point currentPosition; 
InBlock.gif 
InBlock.gif                 public  double ZoomFactor 
InBlock.gif                { 
InBlock.gif                        get {  return _zoom; } 
InBlock.gif                        set { _zoom = value; } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public MultiScaleImage() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         // 
InBlock.gif                         // We are setting the source here, but you should be able to set the Source property via 
InBlock.gif                         // 
InBlock.gif                         this.msi.Source =  new DeepZoomImageTileSource( new Uri( "/DeepZoomImages/dzc_output.xml", UriKind.Relative)); 
InBlock.gif 
InBlock.gif                         // 
InBlock.gif                         // Firing an event when the MultiScaleImage is Loaded 
InBlock.gif                         // 
InBlock.gif                         this.msi.Loaded +=  new RoutedEventHandler(msi_Loaded); 
InBlock.gif 
InBlock.gif                         // 
InBlock.gif                         // Firing an event when all of the images have been Loaded 
InBlock.gif                         // 
InBlock.gif                         this.msi.ImageOpenSucceeded +=  new RoutedEventHandler(msi_ImageOpenSucceeded); 
InBlock.gif 
InBlock.gif                         // 
InBlock.gif                         // Handling all of the mouse and keyboard functionality 
InBlock.gif                         // 
InBlock.gif                         this.MouseMove +=  delegate( object sender, MouseEventArgs e) 
InBlock.gif                        { 
InBlock.gif                                 if (mouseButtonPressed) 
InBlock.gif                                { 
InBlock.gif                                        mouseIsDragging =  true
InBlock.gif                                } 
InBlock.gif                                 this.lastMousePos = e.GetPosition( this.msi); 
InBlock.gif                        }; 
InBlock.gif 
InBlock.gif                         this.MouseLeftButtonDown +=  delegate( object sender, MouseButtonEventArgs e) 
InBlock.gif                        { 
InBlock.gif                                mouseButtonPressed =  true
InBlock.gif                                mouseIsDragging =  false
InBlock.gif                                dragOffset = e.GetPosition( this); 
InBlock.gif                                currentPosition = msi.ViewportOrigin; 
InBlock.gif                        }; 
InBlock.gif 
InBlock.gif                         this.msi.MouseLeave +=  delegate( object sender, MouseEventArgs e) 
InBlock.gif                        { 
InBlock.gif                                mouseIsDragging =  false
InBlock.gif                        }; 
InBlock.gif 
InBlock.gif                         this.MouseLeftButtonUp +=  delegate( object sender, MouseButtonEventArgs e) 
InBlock.gif                        { 
InBlock.gif                                mouseButtonPressed =  false
InBlock.gif                                 if (mouseIsDragging ==  false
InBlock.gif                                { 
InBlock.gif                                         bool shiftDown = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift; 
InBlock.gif 
InBlock.gif                                        ZoomFactor = 2.0; 
InBlock.gif                                         if (shiftDown) ZoomFactor = 0.5; 
InBlock.gif                                        Zoom(ZoomFactor,  this.lastMousePos); 
InBlock.gif                                } 
InBlock.gif                                mouseIsDragging =  false
InBlock.gif                        }; 
InBlock.gif 
InBlock.gif                         this.MouseMove +=  delegate( object sender, MouseEventArgs e) 
InBlock.gif                        { 
InBlock.gif                                 if (mouseIsDragging) 
InBlock.gif                                { 
InBlock.gif                                        Point newOrigin =  new Point(); 
InBlock.gif                                        newOrigin.X = currentPosition.X - (((e.GetPosition(msi).X - dragOffset.X) / msi.ActualWidth) * msi.ViewportWidth); 
InBlock.gif                                        newOrigin.Y = currentPosition.Y - (((e.GetPosition(msi).Y - dragOffset.Y) / msi.ActualHeight) * msi.ViewportWidth); 
InBlock.gif                                        msi.ViewportOrigin = newOrigin; 
InBlock.gif                                } 
InBlock.gif                        }; 
InBlock.gif 
InBlock.gif                         new MouseWheelHelper( this).Moved +=  delegate( object sender, MouseWheelEventArgs e) 
InBlock.gif                        { 
InBlock.gif                                e.Handled =  true
InBlock.gif                                 if (e.Delta > 0) 
InBlock.gif                                        ZoomFactor = 1.2; 
InBlock.gif                                 else 
InBlock.gif                                        ZoomFactor = .80; 
InBlock.gif 
InBlock.gif                                Zoom(ZoomFactor,  this.lastMousePos); 
InBlock.gif                        }; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void msi_ImageOpenSucceeded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         //If collection, this gets you a list of all of the MultiScaleSubImages 
InBlock.gif                         // 
InBlock.gif                         //foreach (MultiScaleSubImage subImage in msi.SubImages) 
InBlock.gif                         //{ 
InBlock.gif                         //        // Do something 
InBlock.gif                         //} 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void msi_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         // Hook up any events you want when the image has successfully been opened 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 public  void Zoom( double zoom, Point pointToZoom) 
InBlock.gif                { 
InBlock.gif                        Point logicalPoint =  this.msi.ElementToLogicalPoint(pointToZoom); 
InBlock.gif                         this.msi.ZoomAboutLogicalPoint(zoom, logicalPoint.X, logicalPoint.Y); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 /* 
InBlock.gif                 *    Sample event handlerrs tied to the Click of event of various buttons for    
InBlock.gif                 *    showing all images, zooming in, and zooming out! 
InBlock.gif                 *    
InBlock.gif                private void ShowAllClick(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        this.msi.ViewportOrigin = new Point(0, 0); 
InBlock.gif                        this.msi.ViewportWidth = 1; 
InBlock.gif                        ZoomFactor = 1; 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void zoomInClick(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        Zoom(1.2, new Point(this.ActualWidth / 2, this.ActualHeight / 2)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                private void zoomOutClick(object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        Zoom(.8, new Point(this.ActualWidth / 2, this.ActualHeight / 2)); 
InBlock.gif                } 
InBlock.gif                 * */
 
InBlock.gif        } 
InBlock.gif}
 
 
 
     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/342827 ,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值