最近,我轉到一間在香港比較大型的軟件公司工作。基於新公司客戶的要求,在工作中是需要我用到微軟Silverlight。所以我嘗試通過互聯網查找一些關於Silverlight的學習資源。但我發現很少香港的公司在他們的業務應用程式中使用 Silverlight。
因此,我去了中央圖書館借了些書,並且到外國的相關技術網站獲取Silverlight一些基本的知識。今天我開始總結一些編寫silverlight時所遇到的程式問題,根據我個人對 Silverlight 研究的經驗,提出一些方法給大家參考。
問題根源: 在一些Silverlight的討論區中,有些人曾詢問在DataGrid內以CellTemplate 和CellEditingTemplate方式加入ComboBox時,遇到了以下問題:
(當我在CellEditingTemplate內的下拉式清單選了的值後,不知道如何才能刷新所對應的CellTempate內 TextBlock值。)
這問題我亦遇到,因此我寫了一個ComboBox在 CellTemplate 和 CellEditingTemplate 之間的示例。下面的代碼示例將向您展示如何解決它。我用的開發環境是 Visual Studio 2010 和 Silverlight 4 版本。
頁面 MainPage.xaml 源碼 ---
1: <UserControl x:Class="SlComboSample1.MainPage"
2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6: mc:Ignorable="d"
7: d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
8:
9: <Grid x:Name="LayoutRoot" Background="White">
10: <sdk:DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="24,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="500" Height="305" CellEditEnded="dataGrid1_CellEditEnded">
11: <sdk:DataGrid.Columns>
12: <sdk:DataGridTemplateColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="PATIENT NAME" >
13: <sdk:DataGridTemplateColumn.CellTemplate>
14: <DataTemplate>
15: <TextBlock Text="{Binding PatientName}" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBlock1" VerticalAlignment="Top" />
16: </DataTemplate>
17: </sdk:DataGridTemplateColumn.CellTemplate>
18: <sdk:DataGridTemplateColumn.CellEditingTemplate>
19: <DataTemplate>
20: <ComboBox ItemsSource="{Binding }" SelectedValuePath="PatientID" DisplayMemberPath="Name" SelectedValue="{Binding Patient,Mode=TwoWay}" Loaded="comboBox2_Loaded" Height="23" HorizontalAlignment="Left" Margin="10,43,0,0" Name="comboBox2" VerticalAlignment="Top" Width="120" />
21: </DataTemplate>
22: </sdk:DataGridTemplateColumn.CellEditingTemplate>
23: </sdk:DataGridTemplateColumn>
24: <sdk:DataGridTextColumn Binding="{Binding DiagnosisResult,Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Diagnosis Result" Width="Auto" />
25: </sdk:DataGrid.Columns>
26: </sdk:DataGrid>
27: </Grid>
28: </UserControl>
背後的 MainPage.xaml.cs 的源碼 ---
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Windows;
5: using System.Windows.Controls;
6:
7: namespace SlComboSample1
8: {
9: public partial class MainPage : UserControl
10: {
11: List<Patient> patientList;
12:
13: public MainPage()
14: {
15: InitializeComponent();
16: // 新增四筆記錄到Patient類別,該類別用作ComboBox的選項來源
17: patientList = new List<Patient>();
18: patientList.Add(new Patient(1, "John"));
19: patientList.Add(new Patient(2, "Sam"));
20: patientList.Add(new Patient(3, "Barry"));
21: patientList.Add(new Patient(4, "Ken"));
22:
23: // 新增三筆Diagnosis記錄到DataGrid
24: List<Diagnosis> DiagnosisList = new List<Diagnosis>();
25: DiagnosisList.Add(new Diagnosis());
26: DiagnosisList.Add(new Diagnosis());
27: DiagnosisList.Add(new Diagnosis());
28: dataGrid1.ItemsSource = DiagnosisList;
29:
30: }
31:
32: private void comboBox2_Loaded(object sender, RoutedEventArgs e)
33: {
34: ((ComboBox)sender).ItemsSource = patientList; //把patientList的集合繫結到ComboBox
35: }
36:
37: private void dataGrid1_CellEditEnded(object sender, DataGridCellEditEndedEventArgs e)
38: {
39:
40: if (e.Column.Header.ToString().Equals("PATIENT NAME"))
41: {
42: List<Diagnosis> dList = ((DataGrid)sender).ItemsSource as List<Diagnosis>;
43: var patientname = from p in (patientList.AsQueryable()) where p.PatientID == (dList[((DataGrid)sender).SelectedIndex].Patient) select p.Name;
44: if (patientname.ToList().Count > 0)
45: {
46: Diagnosis row = (Diagnosis)e.Row.DataContext;
47: row.PatientName = Convert.ToString(patientname.ToList()[0]);
48: }
49: }
50: }
51: }
52:
53: public class Diagnosis
54: {
55: private int patient;
56: private string diagnosisResult;
57: private string patientName;
58:
59: public Diagnosis() // 提供給DataGrid的資料來源類別
60: {
61: patient = 0; // 預設ComboBox的在Page開始時沒有選任何項目
62: diagnosisResult = null;
63: }
64:
65: public int Patient { get { return patient; } set { patient = value; } }
66: public string PatientName { get { return patientName; } set { patientName = value; } }
67: public string DiagnosisResult { get { return diagnosisResult; } set { diagnosisResult = value; } }
68: }
69:
70: public class Patient // 提供給ComboBox 的選項來源類別
71: {
72: private int patientID;
73: private string name;
74:
75: public Patient(int PatientID, string Name)
76: {
77: patientID = PatientID;
78: name = Name;
79: }
80: public int PatientID { get { return patientID; } set { patientID = value; } }
81: public string Name { get { return name; } set { name = value; } }
82: }
83:
84: }
在示列中,我用了dataGrid1的 CellEditEnded事件,並用linq把CellEdtingTemplate 內ComboBox的值,刷新到CellTemplate內的TextBlock中。
以下地址是源碼:
https://docs.google.com/open?id=0BxRiNrIXEFArNnFjVWthendSclNoLWk1Q1Y3T1phZw
沒有留言:
張貼留言