稳扎稳打Silverlight(35) - 3.0控件之ChildWindow, SaveFileDialog, HeaderedItemsControl

简介:
[索引页]
[源码下载]


稳扎稳打Silverlight(35) - 3.0控件之ChildWindow, SaveFileDialog, HeaderedItemsControl, VirtualizingStackPanel


作者: webabcd


介绍
Silverlight 3.0 控件一览:
  • ChildWindow - 用于在父窗体前弹出一个的子窗体
  • SaveFileDialog - 用户发起的保存文件对话框(OpenFileDialog - 打开文件对话框)
  • HeaderedItemsControl - 呈现标题和集合数据的控件
  • VirtualizingStackPanel - 虚拟化的 StackPanel(即仅生成需要显示的 UI 元素。当绑定了大量数据,而某时仅显示其中一小部分的时候,使用此控件则可大幅提高呈现效率) 


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

示例
1、演示 ChildWindow 的应用
ChildWindowDemo.xaml
<navigation:Page x:Class="Silverlight30.Control.ChildWindowDemo"    
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     mc:Ignorable="d" 
                     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
                     d:DesignWidth="640" d:DesignHeight="480" 
                     Title="ChildWindowDemo Page"> 
        <Grid x:Name="LayoutRoot"> 
                <StackPanel> 

                        <Button x:Name="btnChildWindow" Content="Show ChildWindow" Click="btnChildWindow_Click" /> 
                        <Button x:Name="btnCustomChildWindow" Content="Show CustomChildWindow" Click="btnCustomChildWindow_Click" /> 
         
                        <TextBlock x:Name="lblResult" /> 
                         
                </StackPanel> 
        </Grid> 
</navigation:Page>
 
ChildWindowDemo.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 using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Control 
InBlock.gif
InBlock.gif         public partial  class ChildWindowDemo : Page 
InBlock.gif        { 
InBlock.gif                 public ChildWindowDemo() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnChildWindow_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         /* 
InBlock.gif                         * ChildWindow - 在父窗体前显示的子窗体 
InBlock.gif                         *         Title - 子窗体的标题 
InBlock.gif                         *         Content - 子窗体的内容 
InBlock.gif                         *         HasCloseButton - 子窗体上是否要有关闭按钮(右上角的 ×) 
InBlock.gif                         *         OverlayBrush - 子窗体打开后,覆盖在父窗体上的 Brush 
InBlock.gif                         *         OverlayOpacity - 子窗体打开后,覆盖在父窗体上的 Brush 的不透明度 
InBlock.gif                         *         Width - 子窗体的宽 
InBlock.gif                         *         Height - 子窗体的高 
InBlock.gif                         *         Closed事件 - 子窗体关闭后所触发的事件 
InBlock.gif                         *         Show() - 打开(显示)子窗体 
InBlock.gif                         */
 
InBlock.gif 
InBlock.gif                        ChildWindow child =  new ChildWindow(); 
InBlock.gif                        child.Title =  "标题"
InBlock.gif                        child.Content =  "内容"
InBlock.gif                        child.HasCloseButton =  true
InBlock.gif                        child.OverlayBrush =  new SolidColorBrush(Colors.Red); 
InBlock.gif                        child.OverlayOpacity = 0.3; 
InBlock.gif                        child.Width = 320; 
InBlock.gif                        child.Height = 240; 
InBlock.gif                         
InBlock.gif                        child.Show(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void child_Closed( object sender, EventArgs e) 
InBlock.gif                { 
InBlock.gif                         /* 
InBlock.gif                         * ChildWindow.DialogResult - 子窗体传递回来的一个 bool? 值(可以用来描述在子窗体中是单击了“确定”按钮还是“取消”按钮) 
InBlock.gif                         * ChildWindow.DataContext - 子窗体传递回来的数据上下文 
InBlock.gif                         */
 
InBlock.gif 
InBlock.gif                        CustomChildWindow child = sender  as CustomChildWindow; 
InBlock.gif                        MessageBox.Show( string.Format( "DialogResult:{0}; DataContext:{1}", child.DialogResult, child.DataContext)); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnCustomChildWindow_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        CustomChildWindow child =  new CustomChildWindow(); 
InBlock.gif                        child.Closed +=  new EventHandler(child_Closed); 
InBlock.gif                        child.Show(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
CustomChildWindow.xaml(自定义子窗体)
<controls:ChildWindow x:Class="Silverlight30.Control.CustomChildWindow" 
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
                     xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
                     Width="320" Height="240"    
                     Title="我是标题"> 
        <Grid x:Name="LayoutRoot" Margin="2"> 
                <Grid.RowDefinitions> 
                        <RowDefinition /> 
                        <RowDefinition Height="Auto" /> 
                </Grid.RowDefinitions> 

                <Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" /> 
                <Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" /> 
        </Grid> 
</controls:ChildWindow>
 
CustomChildWindow.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 Silverlight30.Control 
InBlock.gif
InBlock.gif         public partial  class CustomChildWindow : System.Windows.Controls.ChildWindow 
InBlock.gif        { 
InBlock.gif                 public CustomChildWindow() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void OKButton_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         /* 
InBlock.gif                         * ChildWindow.DialogResult - 传递给父窗体的一个 bool? 值(可以用来描述在子窗体中是单击了“确定”按钮还是“取消”按钮) 
InBlock.gif                         * ChildWindow.DataContext - 传递给父窗体的数据上下文 
InBlock.gif                         */
 
InBlock.gif 
InBlock.gif                         this.DataContext =  "点击了 OK 按钮"
InBlock.gif                         this.DialogResult =  true
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void CancelButton_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         this.DataContext =  "点击了 Cancel 按钮"
InBlock.gif                         this.DialogResult =  false
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
2、SaveFileDialog 和 OpenFileDialog 的演示
SaveFileDialogDemo.xaml
<navigation:Page x:Class="Silverlight30.Control.SaveFileDialogDemo"    
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     mc:Ignorable="d" 
                     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
                     d:DesignWidth="640" d:DesignHeight="480" 
                     Title="SaveFileDialog Page"> 
        <Grid x:Name="LayoutRoot"> 
                <StackPanel> 
                 
                        <TextBox x:Name="txtInfo" /> 
                        <Button x:Name="btnSave" Content="保存" Click="btnSave_Click" /> 
                        <Button x:Name="btnLoad" Content="载入" Click="btnLoad_Click" /> 
                         
                </StackPanel> 
        </Grid> 
</navigation:Page>
 
SaveFileDialogDemo.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 using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif using System.IO; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Control 
InBlock.gif
InBlock.gif         public partial  class SaveFileDialogDemo : Page 
InBlock.gif        { 
InBlock.gif                 public SaveFileDialogDemo() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnSave_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         /* 
InBlock.gif                         * SaveFileDialog - 用户发起的保存文件对话框 
InBlock.gif                         *         Filter - 指定保存文件的描述信息及文件类型(出现在对话框的“保存类型”下拉列表中) 
InBlock.gif                         *         DefaultExt - 当指定保存文件类型为 *.* 时的默认扩展名 
InBlock.gif                         *         FilterIndex - 默认的保存类型在 Filter 中的索引(注意:索引从 1 开始) 
InBlock.gif                         *         ShowDialog() - 显示保存文件对话框。用户在对话框中单击“保存”则返回 true;单击“取消”或关闭对话框则返回 false 
InBlock.gif                         *         OpenFile() - 打开用户选择的文件,并返回文件流 
InBlock.gif                         */
 
InBlock.gif 
InBlock.gif                        SaveFileDialog dialog =  new SaveFileDialog(); 
InBlock.gif                        dialog.Filter =  "Text Files|*.txt|Log Files|*.log|All Files|*.*"
InBlock.gif                        dialog.FilterIndex = 1; 
InBlock.gif 
InBlock.gif                         bool? result = dialog.ShowDialog(); 
InBlock.gif                         if (result ==  true
InBlock.gif                        { 
InBlock.gif                                 using (Stream stream = dialog.OpenFile()) 
InBlock.gif                                { 
InBlock.gif                                         byte[] info = Encoding.UTF8.GetBytes(txtInfo.Text); 
InBlock.gif                                        stream.Write(info, 0, info.Length); 
InBlock.gif                                } 
InBlock.gif 
InBlock.gif                                txtInfo.Text = ""; 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnLoad_Click( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                         /* 
InBlock.gif                         * OpenFileDialog - 打开文件对话框 
InBlock.gif                         *         Filter - 同 SaveFileDialog 
InBlock.gif                         *         FilterIndex - 同 SaveFileDialog 
InBlock.gif                         *         ShowDialog() - 显示打开文件对话框。用户在对话框中单击“打开”则返回 true;单击“取消”或关闭对话框则返回 false 
InBlock.gif                         *         File - 返回用户所选择文件的的 FileInfo 对象 
InBlock.gif                         *         Multiselect - 选择文件时可否多选 
InBlock.gif                         *         Files - 返回用户所选择文件的的 FileInfo 对象集合 
InBlock.gif                         */
 
InBlock.gif 
InBlock.gif                        OpenFileDialog dialog =  new OpenFileDialog(); 
InBlock.gif                        dialog.Filter =  "Text Files|*.txt"
InBlock.gif                         
InBlock.gif                         if (dialog.ShowDialog() ==  true
InBlock.gif                        { 
InBlock.gif                                 using (FileStream fs = dialog.File.OpenRead()) 
InBlock.gif                                { 
InBlock.gif                                         byte[] buffer =  new  byte[fs.Length]; 
InBlock.gif                                        fs.Read(buffer, 0, buffer.Length); 
InBlock.gif 
InBlock.gif                                        txtInfo.Text = Encoding.UTF8.GetString(buffer, 0, buffer.Length); 
InBlock.gif                                } 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
3、演示 HeaderedItemsControl 的使用
HeaderedItemsControl.xaml
<navigation:Page xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"    x:Class="Silverlight30.Control.HeaderedItemsControl"    
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     mc:Ignorable="d" 
                     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
                     d:DesignWidth="640" d:DesignHeight="480" 
                     Title="HeaderedItemsControl Page"> 
        <Grid x:Name="LayoutRoot"> 
         
                <!-- 
                        HeaderedItemsControl - 呈现标题和集合数据的控件 
                        HeaderedItemsControl.Header, HeaderedItemsControl.HeaderTemplate - 用于显示标题 
                        HeaderedItemsControl.Items, HeaderedItemsControl.ItemTemplate - 用于显示集合数据 
                --> 
         
                <controls:HeaderedItemsControl x:Name="headeredItemsControl" > 
                        <controls:HeaderedItemsControl.Header> 
                                <TextBlock Text="Header" /> 
                        </controls:HeaderedItemsControl.Header> 
                        <controls:HeaderedItemsControl.ItemTemplate> 
                                <DataTemplate> 
                                        <TextBlock Text="{Binding}" /> 
                                </DataTemplate> 
                        </controls:HeaderedItemsControl.ItemTemplate> 
                </controls:HeaderedItemsControl> 
                 
        </Grid> 
</navigation:Page>
 
HeaderedItemsControl.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 using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Control 
InBlock.gif
InBlock.gif         public partial  class HeaderedItemsControl : Page 
InBlock.gif        { 
InBlock.gif                 public HeaderedItemsControl() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(HeaderedItemsControl_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void HeaderedItemsControl_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        List< string> items =  new List< string>(); 
InBlock.gif                         for ( int i = 0; i < 5; i++) 
InBlock.gif                        { 
InBlock.gif                                items.Add(i.ToString().PadLeft(10, '0')); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        headeredItemsControl.ItemsSource = items; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 
4、演示 VirtualizingStackPanel 的应用
VirtualizingStackPanel.xaml
<navigation:Page x:Class="Silverlight30.Control.VirtualizingStackPanel"    
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                     mc:Ignorable="d" 
                     xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
                     d:DesignWidth="640" d:DesignHeight="480" 
                     Title="VirtualizingStackPanel Page"> 
        <Grid x:Name="LayoutRoot"> 
         
                <ListBox x:Name="listBox" VerticalAlignment="Top" HorizontalAlignment="Left"    
                        Height="50" Width="300"> 
                        <ListBox.ItemsPanel> 
                                <ItemsPanelTemplate> 
                                        <!-- 
                                                VirtualizingStackPanel - 虚拟化的 StackPanel(即仅生成需要显示的 UI 元素。当绑定了大量数据,而某时仅显示其中一小部分的时候,使用此控件则可大幅提高呈现效率) 
                                                Orientation - 数据的排列方式(垂直排列或水平排列) 
                                        --> 
                                        <VirtualizingStackPanel Orientation="Horizontal" /> 
                                </ItemsPanelTemplate> 
                        </ListBox.ItemsPanel> 
                        <ListBox.ItemTemplate> 
                                <DataTemplate> 
                                        <TextBlock Text="{Binding}" /> 
                                </DataTemplate> 
                        </ListBox.ItemTemplate> 
                </ListBox> 
                 
        </Grid> 
</navigation:Page>
 
VirtualizingStackPanel.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 using System.Windows.Navigation; 
InBlock.gif 
InBlock.gif namespace Silverlight30.Control 
InBlock.gif
InBlock.gif         public partial  class VirtualizingStackPanel : Page 
InBlock.gif        { 
InBlock.gif                 public VirtualizingStackPanel() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif 
InBlock.gif                         this.Loaded +=  new RoutedEventHandler(VirtualizingStackPanel_Loaded); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 void VirtualizingStackPanel_Loaded( object sender, RoutedEventArgs e) 
InBlock.gif                { 
InBlock.gif                        List< string> items =  new List< string>(); 
InBlock.gif                         for ( int i = 0; i < 3000; i++) 
InBlock.gif                        { 
InBlock.gif                                items.Add(i.ToString().PadLeft(10, '0')); 
InBlock.gif                        } 
InBlock.gif 
InBlock.gif                        listBox.ItemsSource = items; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
 

     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/342751 ,如需转载请自行联系原作者
相关文章
Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象
原文:Silverlight自定义数据绑定控件应该如何处理IEditableObject和IEditableCollectionView对象 原创文章,如需转载,请注明出处。   最近在一直研究Silverlight下的数据绑定控件,发现有这样两个接口IEditableObject 和IEditableCollectionView,记录一下结论,欢迎交流指正。
837 0

热门文章

最新文章