[源码下载]
稳扎稳打Silverlight(35) - 3.0控件之ChilDWindow,SavefileDialog,headeredItemsControl,VirtualizingStackPanel
作者: webabcd
介绍
Silverlight 3.0 控件一览:
ChilDWindow - 用于在父窗体前d出一个的子窗体 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
[源码下载] 总结
以上是内存溢出为你收集整理的稳扎稳打Silverlight(35) - 3.0控件之ChildWindow, SaveFileDialog, HeaderedItemsControl, VirtualizingStackPane全部内容,希望文章能够帮你解决稳扎稳打Silverlight(35) - 3.0控件之ChildWindow, SaveFileDialog, HeaderedItemsControl, VirtualizingStackPane所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)