[源码下载]
稳扎稳打Silverlight(36) - 3.0控件之TreeVIEw,ListBox增强,DataGrID增强,MediaElement增强
作者: webabcd
介绍
Silverlight 3.0 控件一览:
TreeVIEw - 树控件 ListBox - 改进:支持多选 DataGrID - 改进:结合 PagedCollectionVIEw 实现数据分组,增加了一些编辑数据的相关事件,结合 DataAnnotations 实现数据验证,等。。。 MediaElement - 增加了对视频 H.264 编码格式的支持,和对音频 AAC 编码格式的支持
在线DEMO
http://www.cnblogs.com/webabcd/archive/2009/08/04/1538238.html
示例
1、演示 TreeVIEw 的使用
TreeVIEw.xml(数据源)
<? xml version="1.0" enCoding="utf-8" ?>
< root >
< node name ="a level 1" >
< node name ="a level 2" >
< node name ="a level 3" >
< node name ="a level 4" />
</ node >
</ node >
</ node >
< node name ="b level 1" >
< node name ="b level 2" >
< node name ="b level 3" >
< node name ="b level 4" >
< node name ="b level 5" />
</ node >
</ node >
</ node >
</ node >
< node name ="c level 1" >
< node name ="c level 2" >
< node name ="c level 3" >
< node name ="c level 4" />
</ node >
</ node >
</ node >
< node name ="d level 1" >
< node name ="d level 2" >
< node name ="d level 3" />
</ node >
</ node >
</ root >
TreeVIEw.xaml
< navigation:Page x:Class ="Silverlight30.Control.TreeVIEw"
xmlns:controls ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls"
xmlns:common ="clr-namespace:System.windows;assembly=System.windows.Controls"
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 ="TreeVIEw Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel >
< StackPanel.Resources >
<!--
HIErarchicalDataTemplate - 呈现层级数据的数据模板
ItemsSource - 指定下一级数据的数据源
ItemTemplate - 指定下一级数据的数据模板
-->
< common:HIErarchicalDataTemplate x:Key ="childTemplate"
ItemsSource =" {Binding Path=Children} " >
< StackPanel OrIEntation ="Horizontal" >
< CheckBox />
< TextBlock Text =" {Binding Path=Title} " FontStyle ="Italic" />
</ StackPanel >
</ common:HIErarchicalDataTemplate >
< common:HIErarchicalDataTemplate x:Key ="treeTemplate"
ItemsSource =" {Binding Path=Children} "
ItemTemplate =" {StaticResource childTemplate} " >
< TextBlock Text =" {Binding Path=Title} " FontWeight ="Bold" />
</ common:HIErarchicalDataTemplate >
</ StackPanel.Resources >
<!--
ItemsSource - 数据源
ItemTemplate - 指定层级显示数据的模板
-->
< controls:TreeVIEw x:name ="treeVIEw" margin ="5"
ItemsSource =" {Binding} "
ItemTemplate =" {StaticResource treeTemplate} "
selecteditemchanged ="treeVIEw_selecteditemchanged" >
</ controls:TreeVIEw >
<!--
TreeVIEwItem - TreeVIEw 的项
header - 项的标题
headerTemplate - 项的标题模板
-->
< controls:TreeVIEw x:name ="treeVIEw2" margin ="5" >
< controls:TreeVIEwItem header ="level 1" >
< controls:TreeVIEwItem header ="level 2" >
< controls:TreeVIEwItem >
< controls:TreeVIEwItem.headerTemplate >
< DataTemplate >
< TextBlock Text ="level 3" FontWeight ="Bold" />
</ DataTemplate >
</ controls:TreeVIEwItem.headerTemplate >
</ controls:TreeVIEwItem >
</ controls:TreeVIEwItem >
</ controls:TreeVIEwItem >
</ controls:TreeVIEw >
</ StackPanel >
</ GrID >
</ navigation:Page >
TreeVIEw.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.Xml.linq;
using Silverlight30.Model;
namespace Silverlight30.Control
{
public partial class TreeVIEw : Page
@H_735_1404@
{public TreeVIEw()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(TreeVIEw_Loaded);
}
voID TreeVIEw_Loaded(object sender, RoutedEventArgs e)
{
XElement root = XElement.Load("Control/TreeVIEw.xml");
// 构造带层级关系的数据源(递归方式)
var result = LoadData(root);
treeVIEw.DataContext = result;
}
private List<Treeviewmodel> LoadData(XElement root)
{
if (root == null)
return null;
var items = from n in root.Elements("node")
select new Treeviewmodel
{
Title = (string)n.Attribute("name"),
Children = LoadData(n)
};
return items.ToList();
}
private voID treeVIEw_selecteditemchanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
MessageBox.Show(((Treeviewmodel)e.NewValue).Title);
}
}
}
2、演示 ListBox 增加的一个功能:多选
ListBox.xaml
< navigation:Page x:Class ="Silverlight30.Control.ListBox"
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 ="ListBox Page" >
< GrID x:name ="LayoutRoot" >
< StackPanel >
<!--
ListBox - 新增特性:可以多选
SelectionMode - 选择模式 [System.windows.Controls.SelectionMode 枚举]
Single - 只允许单选
Multiple - 可以多选(不需要任何辅助键)
Extended - 可以多选(需要 Ctrl 或 Shift 的配合)
-->
< ListBox x:name ="ListBox" margin ="5" WIDth ="200" Height ="100" SelectionMode ="Extended" >
< ListBox.ItemTemplate >
< DataTemplate >
< StackPanel OrIEntation ="Horizontal" >
< TextBlock Text =" {Binding} " margin ="5" />
</ StackPanel >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >
< button Content ="获取选中项" Click ="button_Click" />
< TextBlock x:name ="lblResult" />
</ StackPanel >
</ GrID >
</ navigation:Page >
ListBox.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 ListBox : Page
{
public ListBox()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(ListBox_Loaded);
}
voID ListBox_Loaded(object sender, RoutedEventArgs e)
{
List<string> items = new List<string>();
for (int i = 0; i < 30; i++)
@H_559_2502@
{items.Add(i.ToString().Padleft(10, '0'));
}
ListBox.ItemsSource = items;
}
private voID button_Click(object sender, RoutedEventArgs e)
{
lblResult.Text = "";
// ListBox.SelectedItems - 选中的对象集合
foreach (string s in ListBox.SelectedItems)
{
lblResult.Text += s + "/r/n";
}
}
}
}
3、演示 DataGrID 的几个新增的功能
DataGrID.xaml
< navigation:Page xmlns:data ="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Data" x:Class ="Silverlight30.Control.DataGrID"
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 ="DataGrID Page" >
< GrID x:name ="LayoutRoot" >
<!--
新增功能:结合 PagedCollectionVIEw 实现数据分组;增加了一些编辑数据的相关事件;结合 DataAnnotations 实现数据验证
-->
< data:DataGrID x:name ="dataGrID" autoGenerateColumns ="False" >
< data:DataGrID.Columns >
< data:DataGrIDTextColumn Binding =" {Binding name} " header ="名字" />
< data:DataGrIDTextColumn Binding =" {Binding DateOfBirth} " header ="生日" />
</ data:DataGrID.Columns >
</ data:DataGrID >
</ GrID >
</ navigation:Page >
DataGrID.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.Xml.linq;
using Silverlight30.Model;
using System.windows.Data;
namespace Silverlight30.Control
{
public partial class DataGrID : Page
{
public DataGrID()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(DataGrID_Loaded);
}
voID DataGrID_Loaded(object sender, RoutedEventArgs e)
{
List<EmployeeModel> employees = new List<EmployeeModel>();
employees.Add(new EmployeeModel { name = "aabb", DateOfBirth = DateTime.Now, Salary = 1111 });
employees.Add(new EmployeeModel { name = "aabc", Salary = 1111 });
employees.Add(new EmployeeModel { name = "abcc", Salary = 1122 });
employees.Add(new EmployeeModel @H_679_3419@{ name = "abbc", Salary = 1122 });
employees.Add(new EmployeeModel { name = "aaab", Salary = 1122 });
employees.Add(new EmployeeModel { name = "bcca", Salary = 1122 });
employees.Add(new EmployeeModel { name = "bbac", Salary = 1133 });
employees.Add(new EmployeeModel { name = "cbaa", Salary = 1133 });
employees.Add(new EmployeeModel { name = "ccaa", Salary = 1133 });
employees.Add(new EmployeeModel { name = "cccb", Salary = 1144 });
employees.Add(new EmployeeModel { name = "cccc", Salary = 1155 });
employees.Add(new EmployeeModel { name = "cabc", Salary = 1155 });
employees.Add(new EmployeeModel { name = "cabb", Salary = 1166 });
// 通过 PagedCollectionVIEw 的 GroupDescriptions 设置需要分组的字段,绑定到 DataGrID 后,DataGrID会自动对数据做分组显示
PagedCollectionVIEw vIEw = new PagedCollectionVIEw(employees);
vIEw.GroupDescriptions.Add(new PropertyGroupDescription("Salary"));
dataGrID.ItemsSource = vIEw;
}
}
}
4、演示 MediaElement 的增强点:以 H.264 编码,MP4为容器做演示
MediaElement.xaml
< navigation:Page x:Class ="Silverlight30.Control.MediaElement"
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 ="MediaElement Page" >
< GrID x:name ="LayoutRoot" >
<!--
增加了对视频 H.264 编码格式的支持,和对音频 AAC 编码格式的支持
本例以 H.264 编码,MP4为容器做演示
-->
< MediaElement x:name ="mediaElement" Source ="/Resource/Demo.mp4" WIDth ="320" Height ="240"
Autoplay ="True" MediaEnded ="mediaElement_MediaEnded" />
</ GrID >
</ navigation:Page >
MediaElement.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 MediaElement : Page
{
public MediaElement()
{
InitializeComponent();
}
private voID mediaElement_MediaEnded(object sender, RoutedEventArgs e)
{
// 重播
mediaElement.Stop();
mediaElement.Play();
}
}
}
OK
[源码下载] 总结
以上是内存溢出为你收集整理的稳扎稳打Silverlight(36) - 3.0控件之TreeView, ListBox增强, DataGrid增强, MediaElement增强全部内容,希望文章能够帮你解决稳扎稳打Silverlight(36) - 3.0控件之TreeView, ListBox增强, DataGrid增强, MediaElement增强所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)