Silverlight 中DataGrid自定义列的绑定问题

前两天因为DataGrid自定义列中Command命令绑定问题,让我耽误了好多时间,今天终于OK了,如果有初学者和我一样的话,可以简单看看

首先大家应该知道绑定不上的原因,因为是在DataGrid里,那么里面的属性,都是来源于当前的DataGrid数据源的,所以说,如果你想获得数据源外的信息就需要上下文代理

首先定义一个类:DataContextProxy继承自FrameworkElement

这个类在博客园中有写的,参考这片博文,在这里谢谢这个博主啦

http://www.cnblogs.com/whinsh/archive/2011/07/06/2098766.html

然后是在页面中引用这个代理

全文代码如下

 这个是页面的信息

View Code
<UserControl x:Class="MVVM3.View.StudentListView"
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"
d:DesignHeight
="300" d:DesignWidth="400"
xmlns:sdk
="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:vm
="clr-namespace:MVVM3.ViewModel" >

<UserControl.Resources>
<vm:DataContextProxy x:Key="DataContextProxy"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Students,Mode=TwoWay}" Height="169" HorizontalAlignment="Left" Margin="46,25,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="227" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="姓名" Binding="{Binding Name,Mode=TwoWay}" ></sdk:DataGridTextColumn>
<sdk:DataGridTextColumn Header="电话" Binding="{Binding Tel,Mode=TwoWay}"></sdk:DataGridTextColumn>
<sdk:DataGridTemplateColumn Header="操作">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="删除" Command="{Binding Path=DataSource.DeleteCommand,Mode=OneWay,Source={StaticResource DataContextProxy}}" CommandParameter="{Binding Name}"></Button>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="44,206,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding QueryCommand}" />
</Grid>
</UserControl>

这个是后台代码

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 MVVM3.ViewModel;

namespace MVVM3.View
{
public partial class StudentListView : UserControl
{
public StudentListView()
{

InitializeComponent();
StudentViewModel svm
= new StudentViewModel();
base.DataContext = svm;
}
}
}

 

这个是StudentViewModel类了

View Code
 1 using System;
2 using System.Net;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Documents;
6 using System.Windows.Ink;
7 using System.Windows.Input;
8 using System.Windows.Media;
9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11 using System.ComponentModel;
12 using MVVM3.Model;
13 using System.Collections.ObjectModel;
14 using GalaSoft.MvvmLight.Command;
15 using MVVM3.View;
16
17 namespace MVVM3.ViewModel
18 {
19 public class StudentViewModel : INotifyPropertyChanged
20 {
21 #region 私有字段
22 private ObservableCollection<Student> students;
23 #endregion
24
25 #region 构造函数
26
27 public StudentViewModel()
28 {
29
30 QueryCommand = new RelayCommand(QueryStudent);
31 DeleteCommand = new RelayCommand<string>(DeteteStudent);
32 }
33 #endregion
34
35 #region 公共属性
36 public ObservableCollection<Student> Students
37 {
38 get { return students; }
39 set
40 {
41 students = value;
42 PropertyChanged(this, new PropertyChangedEventArgs("Students"));
43 }
44 }
45 public event PropertyChangedEventHandler PropertyChanged;
46
47 #region Command
48 /// <summary>
49 /// 查询方法
50 /// </summary>
51 public RelayCommand QueryCommand { get; set; }
52
53 /// <summary>
54 /// 删除方法
55 /// </summary>
56 public RelayCommand<string> DeleteCommand { get; set; }
57 #endregion
58 #endregion
59
60 #region 私有方法
61 /// <summary>
62 /// 查询信息
63 /// </summary>
64 private void QueryStudent()
65 {
66 Students = new ObservableCollection<Student>();
67 Student s1 = new Student("aa","bb");
68 Student s2 = new Student("cc","dd");
69 Students.Add(s1);
70 Students.Add(s2);
71 }
72 /// <summary>
73 /// 删除学生
74 /// </summary>
75 private void DeteteStudent(string name)
76 {
77 MessageBox.Show("删除学生:"+name);
78 }
79 #endregion
80
81 }
82 }

做的不好请多关照,主要是希望和我一样的初学者朋友,能知道自定义列的绑定方法,别无他求!

posted on 2011-09-04 16:34  寇剑  阅读(1636)  评论(0编辑  收藏  举报