My Silverlight系列(5)—— Silverlight控件介绍之TabControl
2009-04-25 12:04:52 来源:WEB开发网后台代码:
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Net;
5using System.Windows;
6using System.Windows.Controls;
7using System.Windows.Documents;
8using System.Windows.Input;
9using System.Windows.Media;
10using System.Windows.Media.Animation;
11using System.Windows.Shapes;
12using System.Collections.ObjectModel;
13
14namespace SilverlightApplication1
15{
16 public partial class Page : UserControl
17 {
18 public Page()
19 {
20 InitializeComponent();
21 this.tab.ItemsSource = source;
22 }
23
24 private ObservableCollection<TabItem> source = new ObservableCollection<TabItem>();
25 /**//*
26 * 这是一种比较强悍的手段,在内部直接维护了一个Dicionary
27 * 当然也可以通过LINQ来查询source来找到相应的TabItem
28 * 这样做的目的是为了在点击Header的"x"时,可以关掉相应的Tab
29 */
30 private Dictionary<Data, TabItem> hash = new Dictionary<Data, TabItem>();
31 private void myButton_Click(object sender, RoutedEventArgs e)
32 {
33 TabItem tabItem = new TabItem();
34 Data data = new Data()
35 {
36 HeaderText = "myHeader",
37 ContentText = "abcd"
38 };
39 tabItem.Style = (Style)(this.tab.Resources["style"]);
40 tabItem.DataContext = data;
41 source.Add(tabItem);
42 hash[data] = tabItem;
43 tab.SelectedItem = tabItem;
44 }
45
46 private void Button_Click(object sender, RoutedEventArgs e)
47 {
48 var btn = sender as Button;
49 if (btn != null)
50 {
51 var data = btn.Tag as Data;
52 source.Remove(hash[data]);
53 }
54 }
55
56 public class Data
57 {
58 public string HeaderText { get; set; }
59 public string ContentText { get; set; }
60 }
61 }
62}
63
Tags:My Silverlight 系列
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接