我是Silverlight初学者,我用的是MVVM模式,遭遇的是这样的问题,在ViewModel层中的Command事件,绑定不到DataGrid中的模板列DataGridTemplateColumn中,我初步推测是资源问题导致的,但是我确实不知道怎么样写啦,大家帮帮忙吧,以下是ViewMode中的代码
1 namespace MVVM3.ViewModel
2 {
3 public class TeacherViewModel : ViewModelBase
4 {
5 TestDBService.Service1Client client;
6 #region 其他信息
7 /// <summary>
8 /// 教师信息集合
9 /// </summary>
10 private ObservableCollection<Teacher> teachers;
11
12 public ObservableCollection<Teacher> Teachers
13 {
14 get { return teachers; }
15 set
16 {
17 teachers = value;
18 base.OnPropertyChanged("Teachers");
19 }
20 }
21 private Teacher myTeacher;
22 public Teacher MyTeacher
23 {
24 get { return myTeacher; }
25 set
26 {
27
28 myTeacher = value;
29 base.OnPropertyChanged("MyTeacher");
30 }
31 }
32
33 public TeacherViewModel()
34 {
35 client = ClientManager.GetPox();
36 teachers = new ObservableCollection<Teacher>();
37 myTeacher = new Teacher();
38 }
39 #region 添加教师信息
40 /// <summary>
41 /// 添加教师信息
42 /// </summary>
43 /// <param name="t">教师实体bean</param>
44 public void AddTeacher(Teacher t)
45 {
46
47 client.AddTeacherCompleted -= new EventHandler<AddTeacherCompletedEventArgs>(client_AddTeacherCompleted);
48 client.AddTeacherCompleted += new EventHandler<AddTeacherCompletedEventArgs>(client_AddTeacherCompleted);
49 client.AddTeacherAsync(t);
50 }
51
52 void client_AddTeacherCompleted(object sender, AddTeacherCompletedEventArgs e)
53 {
54 if (e.Error == null)
55 {
56 int result = e.Result;
57 if (result > 0)
58 {
59 MessageBox.Show("添加教师成功!");
60 }
61 else
62 {
63 MessageBox.Show("添加教师失败!");
64 }
65 }
66 else
67 {
68 MessageBox.Show(e.Error.Message);
69 }
70 }
71
72
73 /// <summary>
74 /// 添加接口
75 /// </summary>
76 public ICommand AddTeacherCommand
77 {
78 get
79 {
80 return new AddTeacherCommand(this);
81 }
82 }
83 #endregion
84 #region 查询接口
85 /// <summary>
86 /// 查询接口
87 /// </summary>
88 public ICommand QueryTeacherCommand
89 {
90 get { return new QueryTeacherCommand(this); }
91 }
92
93
94 public void QueryTeacher()
95 {
96
97 client.GetTeachersCompleted -= new EventHandler<GetTeachersCompletedEventArgs>(client_GetTeachersCompleted);
98 client.GetTeachersCompleted += new EventHandler<GetTeachersCompletedEventArgs>(client_GetTeachersCompleted);
99 client.GetTeachersAsync();
100 }
101
102 void client_GetTeachersCompleted(object sender, GetTeachersCompletedEventArgs e)
103 {
104 if (e.Error == null)
105 {
106 Teachers = e.Result;
107 }
108 }
109 #endregion
110 #endregion
111 #region 删除接口
112 public ICommand DelTeacherCommand
113 {
114 get { return new DelTeacherCommand(this); }
115 }
116
117 public void DelTeacher(int id)
118 {
119 client.DelTeacherCompleted -= new EventHandler<DelTeacherCompletedEventArgs>(client_DelTeacherCompleted);
120 client.DelTeacherCompleted += new EventHandler<DelTeacherCompletedEventArgs>(client_DelTeacherCompleted);
121 client.DelTeacherAsync(id);
122 }
123
124 void client_DelTeacherCompleted(object sender, DelTeacherCompletedEventArgs e)
125 {
126 if (e.Error == null)
127 {
128 int result = e.Result;
129 if (result == 0)
130 {
131 MessageBox.Show("删除失败!");
132 }
133 else
134 {
135 MessageBox.Show("删除成功!");
136 }
137 }
138 else
139 {
140 MessageBox.Show(e.Error.Message);
141 }
142 }
143
144 #endregion
145 }
146 }
这个是View中的代码
1 <navigation:Page x:Class="MVVM3.View.TeacherListView"
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 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
7 mc:Ignorable="d"
8 xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
9 d:DesignWidth="640" d:DesignHeight="480"
10 Title="TeacherListView Page" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
11 xmlns:vm="clr-namespace:MVVM3.ViewModel"
12 >
13 <UserControl.Resources>
14 <vm:TeacherViewModel x:Key="tvm"/>
15
16 </UserControl.Resources>
17
18 <Grid x:Name="LayoutRoot" DataContext="{StaticResource tvm}">
19 <sdk:DataGrid AutoGenerateColumns="False" Height="120" HorizontalAlignment="Left" Margin="14,9,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="165" ItemsSource="{Binding Teachers}" >
20
21 <sdk:DataGrid.Columns>
22 <sdk:DataGridTextColumn Header="姓名" Binding="{Binding Name}"></sdk:DataGridTextColumn>
23 <sdk:DataGridTextColumn Header="科目" Binding="{Binding Subject}"></sdk:DataGridTextColumn>
24 <sdk:DataGridTemplateColumn Header="操作">
25 <sdk:DataGridTemplateColumn.CellTemplate>
26 <DataTemplate>
27 <Button Content="{Binding ID}" Command="{Binding RelativeSource=DelTeacherCommand}" CommandParameter="{Binding Id}" ></Button>
28 </DataTemplate>
29 </sdk:DataGridTemplateColumn.CellTemplate>
30 </sdk:DataGridTemplateColumn>
31 </sdk:DataGrid.Columns>
32 </sdk:DataGrid>
33 <Button Content="查询" Height="23" HorizontalAlignment="Left" Margin="14,135,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding QueryTeacherCommand}" />
34 </Grid>
35 </navigation:Page>