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

[索引页]
[源码下载]


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


作者: webabcd


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


在线DEMO
http://www.cnblogs.com/webabcd/archive/2009/08/04/1538238.html


示例
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

using  System;

using  System.Collections.Generic;

using  System.Linq;

using  System.Net;

using  System.Windows;

using  System.Windows.Controls;

using  System.Windows.Documents;

using  System.Windows.Input;

using  System.Windows.Media;

using  System.Windows.Media.Animation;

using  System.Windows.Shapes;

using  System.Windows.Navigation;


namespace  Silverlight30.Control

{

    
public partial class ChildWindowDemo : Page

    
{

        
public ChildWindowDemo()

        
{

            InitializeComponent();

        }


        
private void btnChildWindow_Click(object sender, RoutedEventArgs e)

        
{

            
/*

             * ChildWindow - 在父窗体前显示的子窗体

             *     Title - 子窗体的标题

             *     Content - 子窗体的内容

             *     HasCloseButton - 子窗体上是否要有关闭按钮(右上角的 ×)

             *     OverlayBrush - 子窗体打开后,覆盖在父窗体上的 Brush

             *     OverlayOpacity - 子窗体打开后,覆盖在父窗体上的 Brush 的不透明度

             *     Width - 子窗体的宽

             *     Height - 子窗体的高

             *     Closed事件 - 子窗体关闭后所触发的事件

             *     Show() - 打开(显示)子窗体

             
*/


            ChildWindow child 
= new ChildWindow();

            child.Title 
= "标题";

            child.Content 
= "内容";

            child.HasCloseButton 
= true;

            child.OverlayBrush 
= new SolidColorBrush(Colors.Red);

            child.OverlayOpacity 
= 0.3;

            child.Width 
= 320;

            child.Height 
= 240;

            

            child.Show();

        }


        
void child_Closed(object sender, EventArgs e)

        
{

            
/*

             * ChildWindow.DialogResult - 子窗体传递回来的一个 bool? 值(可以用来描述在子窗体中是单击了“确定”按钮还是“取消”按钮)

             * ChildWindow.DataContext - 子窗体传递回来的数据上下文

             
*/


            CustomChildWindow child 
= sender as CustomChildWindow;

            MessageBox.Show(
string.Format("DialogResult:{0}; DataContext:{1}", child.DialogResult, child.DataContext));

        }


        
private void btnCustomChildWindow_Click(object sender, RoutedEventArgs e)

        
{

            CustomChildWindow child 
= new CustomChildWindow();

            child.Closed 
+= new EventHandler(child_Closed);

            child.Show();

        }

    }

}


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"  Grid.Row ="1"   />

        
< Button  x:Name ="OKButton"  Content ="OK"  Click ="OKButton_Click"  Width ="75"  Height ="23"  HorizontalAlignment ="Right"  Margin ="0,79,0"  Grid.Row ="1"   />

    
</ Grid >

</ controls:ChildWindow >

CustomChildWindow.xaml.cs

using  System;

using  System.Collections.Generic;

using  System.Linq;

using  System.Net;

using  System.Windows;

using  System.Windows.Controls;

using  System.Windows.Documents;

using  System.Windows.Input;

using  System.Windows.Media;

using  System.Windows.Media.Animation;

using  System.Windows.Shapes;


namespace  Silverlight30.Control

{

    
public partial class CustomChildWindow : System.Windows.Controls.ChildWindow

    
{

        
public CustomChildWindow()

        
{

            InitializeComponent();

        }


        
private void OKButton_Click(object sender, RoutedEventArgs e)

        
{

            
/*

             * ChildWindow.DialogResult - 传递给父窗体的一个 bool? 值(可以用来描述在子窗体中是单击了“确定”按钮还是“取消”按钮)

             * ChildWindow.DataContext - 传递给父窗体的数据上下文

             
*/


            
this.DataContext = "点击了 OK 按钮";

            
this.DialogResult = true;

        }


        
private void CancelButton_Click(object sender, RoutedEventArgs e)

        
{

            
this.DataContext = "点击了 Cancel 按钮";

            
this.DialogResult = false;

        }

    }

}




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

using  System;

using  System.Collections.Generic;

using  System.Linq;

using  System.Net;

using  System.Windows;

using  System.Windows.Controls;

using  System.Windows.Documents;

using  System.Windows.Input;

using  System.Windows.Media;

using  System.Windows.Media.Animation;

using  System.Windows.Shapes;

using  System.Windows.Navigation;


using  System.IO;

using  System.Text;


namespace  Silverlight30.Control

{

    
public partial class SaveFileDialogDemo : Page

    
{

        
public SaveFileDialogDemo()

        
{

            InitializeComponent();

        }


        
private void btnSave_Click(object sender, RoutedEventArgs e)

        
{

            
/*

             * SaveFileDialog - 用户发起的保存文件对话框

             *     Filter - 指定保存文件的描述信息及文件类型(出现在对话框的“保存类型”下拉列表中)

             *     DefaultExt - 当指定保存文件类型为 *.* 时的默认扩展名

             *     FilterIndex - 默认的保存类型在 Filter 中的索引(注意:索引从 1 开始)

             *     ShowDialog() - 显示保存文件对话框。用户在对话框中单击“保存”则返回 true;单击“取消”或关闭对话框则返回 false

             *     OpenFile() - 打开用户选择的文件,并返回文件流

             
*/


            SaveFileDialog dialog 
= new SaveFileDialog();

            dialog.Filter 
= "Text Files|*.txt|Log Files|*.log|All Files|*.*";

            dialog.FilterIndex 
= 1;


            
bool? result = dialog.ShowDialog();

            
if (result == true)

            
{

                
using (Stream stream = dialog.OpenFile())

                
{

                    
byte[] info = Encoding.UTF8.GetBytes(txtInfo.Text);

                    stream.Write(info, 
0, info.Length);

                }


                txtInfo.Text 
= "";

            }

        }


        
private void btnLoad_Click(object sender, RoutedEventArgs e)

        
{

            
/*

             * OpenFileDialog - 打开文件对话框

             *     Filter - 同 SaveFileDialog

             *     FilterIndex - 同 SaveFileDialog

             *     ShowDialog() - 显示打开文件对话框。用户在对话框中单击“打开”则返回 true;单击“取消”或关闭对话框则返回 false

             *     File - 返回用户所选择文件的的 FileInfo 对象

             *     Multiselect - 选择文件时可否多选

             *     Files - 返回用户所选择文件的的 FileInfo 对象集合

             
*/


            OpenFileDialog dialog 
= new OpenFileDialog();

            dialog.Filter 
= "Text Files|*.txt";

            

            
if (dialog.ShowDialog() == true)

            
{

                
using (FileStream fs = dialog.File.OpenRead())

                
{

                    
byte[] buffer = new byte[fs.Length];

                    fs.Read(buffer, buffer.Length);


                    txtInfo.Text 
= Encoding.UTF8.GetString(buffer, buffer.Length);

                }

            }

        }

    }

}



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

using  System;

using  System.Collections.Generic;

using  System.Linq;

using  System.Net;

using  System.Windows;

using  System.Windows.Controls;

using  System.Windows.Documents;

using  System.Windows.Input;

using  System.Windows.Media;

using  System.Windows.Media.Animation;

using  System.Windows.Shapes;

using  System.Windows.Navigation;


namespace  Silverlight30.Control

{

    
public partial class HeaderedItemsControl : Page

    
{

        
public HeaderedItemsControl()

        
{

            InitializeComponent();


            
this.Loaded += new RoutedEventHandler(HeaderedItemsControl_Loaded);

        }


        
void HeaderedItemsControl_Loaded(object sender, RoutedEventArgs e)

        
{

            List
<string> items = new List<string>();

            
for (int i = 0; i < 5; i++)

            
{

                items.Add(i.ToString().PadLeft(
10'0'));

            }


            headeredItemsControl.ItemsSource 
= items;

        }

    }

}



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

using  System;

using  System.Collections.Generic;

using  System.Linq;

using  System.Net;

using  System.Windows;

using  System.Windows.Controls;

using  System.Windows.Documents;

using  System.Windows.Input;

using  System.Windows.Media;

using  System.Windows.Media.Animation;

using  System.Windows.Shapes;

using  System.Windows.Navigation;


namespace  Silverlight30.Control

{

    
public partial class VirtualizingStackPanel : Page

    
{

        
public VirtualizingStackPanel()

        
{

            InitializeComponent();


            
this.Loaded += new RoutedEventHandler(VirtualizingStackPanel_Loaded);

        }


        
void VirtualizingStackPanel_Loaded(object sender, RoutedEventArgs e)

        
{

            List
<string> items = new List<string>();

            
for (int i = 0; i < 3000; i++)

            
{

                items.Add(i.ToString().PadLeft(
10'0'));

            }


            listBox.ItemsSource 
= items;

        }

    }

}



OK
[源码下载]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


如何在Silverlight4(XAML)中绑定IsEnabled属性?我试过简单的IsEnabled=“{BindingABC}”,但这不起作用–MenuItem总是启用.提前感谢您的线索!干杯编辑:有趣的是,当设置Mode=TwoWay时,绑定似乎有效.但是,在菜单项上移动鼠标后,将更新上下文菜单的外观.这是异步工作吗?右键
我正在编写我的第一个vb.net应用程序(但我也会在这里标记c#,因为我确信即使是一个c#人也可以使用类似的.net实现来回答这个问题).我申请的简短说明:我的桌面应用程序将仅在win平台上运行,使用vb.net,它是一个简单的网吧管理软件,在服务器上运行服务器gui,在工作站上运行客户端gui,
ProcessFile()是在UIThread上运行还是在单独的线程上运行.如果它在UIThread上,如何将文件请求和ProcessFile()移动到单独的线程?varxClient=newServiceReference1.Service1SoapClient();xClient.Retrieve_File_Completed+=newEventHandler<ServiceReference1.Retrieve_Fi
我从同行那里听说,对sharepoint的了解对职业生涯有益.我们不在办公室使用sharepoint.所以不知道如何开始.这些是我的新手问题>学习共享点值得努力吗?>学习sharepoint的资源在哪里?>我是否应该考虑开发哪些参考项目?感谢您的意见.解决方法:SharePoint以如此积极的方式改变了我的职业
我正在尝试保存一个类我的类对象的集合.我收到一个错误说明:Thecollectiondatacontracttype‘System.Collections.Generic.Listcannotbedeserializedbecauseitdoesnothaveapublicparameterlessconstructor.Addingapublicparameterlessconstructorwillf
我需要根据Silverlight中的某些配置值设置给定控件的Style.我想有可能从两种可用的样式(静态资源)中选择一个控件样式.我试图做一些像:<TextBoxStyle="{BindingABC}"/>哪里publicstringABC{get{return"{StaticResourceMyStyle}";}}不幸的是,这不起作用.你有
我刚买了第一台Android设备,我喜欢它…我也很喜欢你可以创建自己的应用程序并随意分发它们.我已经阅读了一些关于Monodroid的内容,而且显然微软希望将Silverlight放在这些设备上,尽管没有太多关于它们的信息……但是Moonlight呢?如果Monodroid就像Mono……为什么我们需要它呢?相反
我们的ASP.NET网站允许用户执行各种查询,并根据从数据库查询的结果显示网络图(如UML图).目前,我们正在生成一个位图并显示它.但由于我们需要支持允许用户以交互方式显示/隐藏某些块的功能,因此我们计划使用Silverlight来渲染图形.我们还计划在未来添加更多互动.我有两个问题:>ASP
我正在开发一个Silverlight4应用程序,我已经创建了一个自定义的启动画面.乍一看,自定义启动画面运行良好–非常好.几天后,我开始注意到闪屏不再显示,屏幕仍然是空白.这似乎只发生在我打开多个指向同一个应用程序的IE选项卡/窗口时.前几个将加载正常,而以下选项卡/窗口将保持“白
这是我的XAML:<ImageVerticalAlignment="Center"HorizontalAlignment="Center"Source="{BindingInput,Converter={StaticResourceByteArrayToBitmapConverter}}"><Image.Rend
问候,我有一个ItemsControl,我更改了模板,为绑定的ItemsSource中的每个对象显示一个RadioButton.但是ItemsSource可以为空,当它为空时我想显示默认值.像“绑定列表中没有可供您选择的项目”……我想到的一种方法是将ItemsControl.Visibility设置为Collapsed,并将TextBlock.Vsibi
堆栈溢出的第一个问题……我是C#的新手,但在学习它时却非常直接.几分钟前我才看到这个tutorial.通过各种c#技术阅读WCF,WPF,Silverlight,c#和asp.net,这是很多技术都可以用c#来实现.我将创建一个Web应用程序c#.我认为SilverLight似乎是我最好的选择.该应用程序将拥有一个数
我正在使用MVVM(Model-View-ViewModel)模式编写应用程序,并利用MicrosoftP&P团队的Prism和Unity位.我有一个包含项目列表的视图.这些项包含在ViewModel中的ObservableCollection中,View中的列表框是数据绑定的(ViewModel设置为View的DataContext).在ViewModel中,我有一个运行的
我有一个应用程序,它在首次运行时显示免责声明页面.选择“接受”或“拒绝”后,您再也看不到该页面了.但是,当您在第一次运行后按后退键尝试关闭应用程序时,您将返回免责声明页面,然后再次点击该页面,返回主页面,然后再次退出.这仅在应用程序第一次运行时发生,但我希望应用程序在
我正在尝试在SilverlightforWindowsPhone中使用异步HttpWebRequest.一切都很完美,直到我到达我应该打电话的地方privatestaticManualResetEventallDone=newManualResetEvent(false);...request.BeginGetResponse(newAsyncCallback(GetResponseCallback),request);a
嗨,我有两个Writablebitmap,一个来自jpg,另一个来自png,并使用此方法在循环中混合颜色:privatestaticColorMix(Colorfrom,Colorto,floatpercent){floatamountFrom=1.0f-percent;returnColor.FromArgb((byte)(from.A*amountFrom+to.A*perc
我需要开发一个程序,它包含一个图像(png),中心有一个洞.在这个图像下将有一个框架,我想点击图像的透明孔我可以点击框架内的按钮.我不知道是否有一种方法可以通过图像或其他方式传播点击.谢谢你的帮助解决方法:你在图像上将IsHitTestVisible设置为false,然后点击浏览.
我正在研究一个silverlight应用程序,我发现List没有Find扩展方法说,List<Something>list=newList<Something>(something);list.Remove(list.Find(e=>e.id==10));没有查找扩展方法我错过了什么?解决方法:它不包括在内以减小运行时的大小.建议您使用LINQ扩展,例如First
我试图弄清楚如何设置Path元素的Data属性来获得此类型的软角:alttexthttp://i42.tinypic.com/1rzu6w.jpg现在我只有这样的尖角:alttexthttp://i42.tinypic.com/2eeleah.jpg我尝试用椭圆玩,但我无法得到我想要的东西.谢谢最佳答案:路径的段具有IsSmoothJoin属性,默认为false.
问题我有一个在远程服务器上运行的restfulWeb服务.我已经制作了一个使用它的WP7应用程序,所以我知道它有效.我正在将应用程序移植到SilverlightWeb应用程序并遇到问题.我已经包含了代码的简化版本以及引发的错误.EndGetResponse方法抛出错误.随意询问更多信息.我一直在寻找解