大菜博客园之处女作:silverlight 行为浅谈1-----------加载数据
大哥大嫂们,先听小弟小小扯淡一下,算是对咱处女作的支持把...............
小弟工作两年,至今仍觉的技术太差,来新公司就直接接触了silverlight 4 ,根本就没有silverlight 1/ 2/3 的基础,以前只是做web 开发,winform都没开发过具体的项目,都是自己做着玩。公司要开发新项目,决定用EF4 和 行为(汗....一个也没接触过)。
闲话少说,先来看效果
前提:小弟只是做了个Demo
具体怎么新建 silverlight项目 等操作,请上Google查
datagrid 列定义
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Binding="{Binding Id}" Header="ID" Width="200"/>
<sdk:DataGridTextColumn Binding="{Binding Name}" Header="姓名" Width="100" />
<sdk:DataGridTextColumn Binding="{Binding ClassName}" Header="班级" Width="80" />
</sdk:DataGrid.Columns>
由于是Demo请原谅小弟懒散...哈哈...话说回来,谁不懒呀....(给自己找个借口)
用vs 2010 新建silverlight类库,然后用blend 4 打开项目,在新添加的类库中添加一个Actions 文件(后缀也是.cs ,感觉其实就是类),可以看到默认生成的代码:
我们修改一下类名,继承 TargetedTriggerAction<UIElement>,也是为了方便添加元素的load 等事件,如下:
public class Student_Action : TargetedTriggerAction<UIElement>
为了在界面加载的时候,让datagrid 就加载数据和方便传值等操作,所以定义了一系列的依赖属性:
#region 定义依赖属性 也就是传值
/// <summary>
/// 行为的命令,为了同一个行为执行不同操作
/// </summary>
public static readonly DependencyProperty CommandNameProperty =
DependencyProperty.Register("CommandName", typeof(String), typeof(Student_Action), null);
public String CommandName
{
get { return (String)GetValue(Student_Action.CommandNameProperty); }
set { SetValue(Student_Action.CommandNameProperty, value); }
}
/// <summary>
/// id
/// </summary>
public static readonly DependencyProperty IdProperty =
DependencyProperty.Register("Id", typeof(String ), typeof(Student_Action), null);
public String Id
{
get { return (String)GetValue(Student_Action.IdProperty); }
set { SetValue(Student_Action.IdProperty, value); }
}
/// <summary>
/// 姓名
/// </summary>
public static readonly DependencyProperty StudentNameProperty =
DependencyProperty.Register("StudentName", typeof(String), typeof(Student_Action), null);
public String StudentName
{
get { return (String)GetValue(Student_Action.StudentNameProperty); }
set { SetValue(Student_Action.StudentNameProperty, value); }
}
/// <summary>
/// 班级
/// </summary>
public static readonly DependencyProperty ClassNameProperty =
DependencyProperty.Register("ClassName", typeof(String), typeof(Student_Action), null);
public String ClassName
{
get { return (String)GetValue(Student_Action.ClassNameProperty); }
set { SetValue(Student_Action.ClassNameProperty, value); }
}
/// <summary>
/// 显示数据的datagrid
/// </summary>
public static readonly DependencyProperty DataObjectProperty =
DependencyProperty.Register("DataObject", typeof(System.Windows.Controls.DataGrid), typeof(Student_Action),null );
public DataGrid DataObject
{
get { return (DataGrid)GetValue(Student_Action.DataObjectProperty); }
set { SetValue(Student_Action.DataObjectProperty, value); }
}
#endregion
又写了一个固定的数据源,(students的定义未写,就是几个属性)
List<Students> DTO = new List<Students>();
public Student_Action()
{
// 在此点下面插入创建对象所需的代码。
for (int a = 1; a < 10; a++)
{
Students stu = new Students();
stu.Id = a;
stu.Name = "姓名" + a.ToString();
stu.ClassName = "班级" + a.ToString();
DTO.Add(stu);
}
}
要想加载数据,需要重写OnAttached方法,在这里边定义事件等..:(感觉加载数据写在按钮的加载事件里不好,应该写在datagrid的加载事件里,如又兴趣可以自己试一下)
protected override void OnAttached()
{
base.OnAttached();
(this.AssociatedObject as Button).Loaded += new RoutedEventHandler(Student_Action_Loaded);
(this.AssociatedObject as Button).Click += new RoutedEventHandler(Student_Action_Click);
}
void Student_Action_Loaded(object sender, RoutedEventArgs e)
{
(this.DataObject as DataGrid).ItemsSource = DTO;
}
写到这里运行项目,变会加载出数据来。
好了,先写到这把,下一篇会把添加数据的写出来,
哎呀,My God ,第一次写博客,还真不知道该怎么写,请大家原谅小弟写的粗糙,没有头绪,欢迎大哥大嫂,指点批评,好了!!!