稳扎稳打Silverlight(62) - 5.0控件之PivotViewer, ListBox 和 ComboBox 新特性, OpenFileDialog 和 SaveFileDialog 新特性
稳扎稳打Silverlight(62) - 5.0控件之PivotViewer, ListBox 和 ComboBox 新特性, OpenFileDialog 和 SaveFileDialog 新特性
作者:webabcd
介绍
Silverlight 5.0 控件
- ListBox 和 ComboBox 新特性 - 支持键盘检索
- OpenFileDialog 和 SaveFileDialog 新特性 - InitialDirectory 和 DefaultFileName
- 新增控件 - PivotViewer
在线DEMO
http://www.cnblogs.com/webabcd/archive/2012/03/05/2379862.html
示例
1、ListBox 和 ComboBox 新特性
Control/ListBoxAndComboBox.xaml
<navigation:Page x:Class="Silverlight50.Control.ListBoxAndComboBox"
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="ListBoxAndComboBox Page">
<StackPanel x:Name="LayoutRoot" HorizontalAlignment="Left">
<!--
ListBox 和 ComboBox 支持键盘搜索(把焦点放到 ListBox,然后可以测试键盘搜索的功能)
-->
<ListBox Name="listBox" Margin="5" Width="200" Height="100">
<ListBoxItem Content="aaa" />
<ListBoxItem Content="bbb" />
<ListBoxItem Content="ccc" />
<ListBoxItem Content="ddd" />
<ListBoxItem Content="eee" />
<ListBoxItem Content="fff" />
<ListBoxItem Content="ggg" />
<ListBoxItem Content="hhh" />
<ListBoxItem Content="iii" />
<ListBoxItem Content="jjj" />
</ListBox>
</StackPanel>
</navigation:Page>
2、OpenFileDialog 和 SaveFileDialog 新特性
Control/OpenFileDialogAndSaveFileDialog.xaml
<navigation:Page x:Class="Silverlight50.Control.OpenFileDialogAndSaveFileDialog"
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="OpenFileDialogAndSaveFileDialog 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>
Control/OpenFileDialogAndSaveFileDialog.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.Text;
using System.IO;
namespace Silverlight50.Control
{
public partial class OpenFileDialogAndSaveFileDialog : Page
{
public OpenFileDialogAndSaveFileDialog()
{
InitializeComponent();
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
/*
* SaveFileDialog - 用户发起的保存文件对话框
* Filter - 指定保存文件的描述信息及文件类型(出现在对话框的“保存类型”下拉列表中)
* DefaultExt - 当指定保存文件类型为 *.* 时的默认扩展名
* SafeFileName - 获取 SaveFileDialog 相关的文件名(只有文件名和扩展名,没有路径信息)
* FilterIndex - 默认的保存类型在 Filter 中的索引(注意:索引从 1 开始)
* ShowDialog() - 显示保存文件对话框。用户在对话框中单击“保存”则返回 true;单击“取消”或关闭对话框则返回 false
* OpenFile() - 打开用户选择的文件,并返回文件流
*/
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*";
dialog.FilterIndex = 1;
// DefaultFileName - 用于设置 SaveFileDialog 的默认文件名(新增属性)
dialog.DefaultFileName = "temp.txt";
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";
// InitialDirectory - 用于设置 OpenFileDialog 的初始目录(新增属性)
dialog.InitialDirectory = @"C:\";
if (dialog.ShowDialog() == true)
{
using (FileStream fs = dialog.File.OpenRead())
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
txtInfo.Text = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
}
}
}
}
}
3、新增控件 PivotViewer
Control/PivotViewerDemo.xaml
<navigation:Page x:Class="Silverlight50.Control.PivotViewerDemo"
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="PivotViewerDemo Page"
xmlns:sdk="clr-namespace:System.Windows.Controls.Pivot;assembly=System.Windows.Controls.Pivot">
<Grid x:Name="LayoutRoot" Background="White">
<!--
更多 PivotViewer 的相关内容参看
http://www.silverlight.net/learn/data-networking/pivot-viewer
-->
<sdk:PivotViewer x:Name="pivotViewer">
<!--
设置 PivotProperties,可用于左侧检索栏
-->
<sdk:PivotViewer.PivotProperties>
<sdk:PivotViewerStringProperty Id="Category" Options="CanFilter" DisplayName="产品类别" Binding="{Binding Category}" />
</sdk:PivotViewer.PivotProperties>
<!--
设置 PivotViewerItemTemplate,用于主显示区
-->
<sdk:PivotViewer.ItemTemplates>
<sdk:PivotViewerItemTemplate>
<Border Width="200" Height="200" Background="Blue">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}" FontSize="16" Foreground="White" />
<TextBlock Text="{Binding Category}" FontSize="16" Foreground="White" />
<TextBlock Text="{Binding Price}" FontSize="16" Foreground="White" />
</StackPanel>
</Border>
</sdk:PivotViewerItemTemplate>
</sdk:PivotViewer.ItemTemplates>
</sdk:PivotViewer>
</Grid>
</navigation:Page>
Control/PivotViewerDemo.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.Collections.ObjectModel;
namespace Silverlight50.Control
{
public partial class PivotViewerDemo : Page
{
public PivotViewerDemo()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ObservableCollection<Product> products = new ObservableCollection<Product>();
Random random = new Random();
// 构造数据源
for (int i = 0; i < 100; i++)
{
products.Add
(
new Product
(
"webabcd" + i.ToString(),
"category" + random.Next(0, 9).ToString(),
random.NextDouble() * 1000d
)
);
}
// 给 PivotViewer 指定数据源
pivotViewer.ItemsSource = products;
}
}
/// <summary>
/// 数据的实体类
/// </summary>
public class Product
{
public string Name { get; set; }
public string Category { get; set; }
public double Price { get; set; }
public Product(string name, string category, double price)
{
this.Name = name;
this.Category = category;
this.Price = price;
}
}
}
OK
[源码下载]