Siverlight dataform 使用小结
2012-04-19 10:01 by swarb, ... 阅读, ... 评论, 收藏, 编辑WCF服务中获取EntityQuery方法:
public UserContext db = new UserContext();
public EntityQuery<userinfo> GetUsers()
{
return db.GetUserinfoesQuery();
}
siverlight调用并绑定dataform
UserRIA ur = new UserRIA();
private void dfUser_Loaded(object sender, RoutedEventArgs e)
{
//var aa = ur.db.Load(ur.GetUsers(), Load,null).Entities;
//dfUser.ItemsSource = aa;
//dfUser.CurrentItem = aa.First();
//dfUser.ItemsSource = GetUsers();
ur.db.Load(ur.GetUsers()).Completed +=new EventHandler(TestDataForm_Completed);
}
void TestDataForm_Completed(object sender, EventArgs e)
{
dfUser.ItemsSource = ur.db.userinfos;
}
private void Load(System.ServiceModel.DomainServices.Client.LoadOperation load)
{
dfUser.CurrentItem = load.Entities.First();
}
siverlight页面dataform设计
引入命名空间: xmlns:dataform="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.DataForm.Toolkit"
添加dataform
<dataform:DataForm x:Name="dfUser" Loaded="dfUser_Loaded" DataContext="{Binding}">
///头模板
<dataform:DataForm.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="用户信息列表"></TextBlock>
</StackPanel>
</DataTemplate>
</dataform:DataForm.HeaderTemplate>
编辑模板
<dataform:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="编辑"></TextBlock>
<dataform:DataField Label=" 用户名">
<TextBox Text="{Binding username,Mode=TwoWay}"></TextBox>
</dataform:DataField>
<dataform:DataField Label=" 密码">
<TextBox Text="{Binding userpwd,Mode=TwoWay}"></TextBox>
</dataform:DataField>
<Button x:Name="btn_Save" Content="保存修改" Click="btn_Save_Click" Margin="10,0,0,0" VerticalAlignment="Center"/>
<Button x:Name="btn_Cancel" Content="取消" Click="btn_Cancel_Click" Margin="10,0,0,0" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</dataform:DataForm.EditTemplate>
/添加模板
<dataform:DataForm.NewItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="添加"></TextBlock>
<dataform:DataField Label=" 用户名">
<TextBox Text="{Binding username,Mode=TwoWay}"></TextBox>
</dataform:DataField>
<dataform:DataField Label=" 密码">
<TextBox Text="{Binding userpwd,Mode=TwoWay}"></TextBox>
</dataform:DataField>
</StackPanel>
</DataTemplate>
</dataform:DataForm.NewItemTemplate>
只读模板
<dataform:DataForm.ReadOnlyTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="只读"></TextBlock>
<dataform:DataField Label=" 用户名">
<TextBox Text="{Binding username,Mode=TwoWay}"></TextBox>
</dataform:DataField>
<dataform:DataField Label=" 密码">
<TextBox Text="{Binding userpwd,Mode=TwoWay}"></TextBox>
</dataform:DataField>
</StackPanel>
</DataTemplate>
</dataform:DataForm.ReadOnlyTemplate>
</dataform:DataForm>
//修改dataform 数据
private void btn_Save_Click(object sender, RoutedEventArgs e)
{
userinfo us = dfUser.CurrentItem as userinfo;
dfUser.CommitEdit();
if (ur.db.HasChanges)
{
ur.db.SubmitChanges();
dfUser.ApplyTemplate();
MessageBox.Show("ok");
}
else
{
MessageBox.Show("无修改项!");
}
}
//取消修改
private void btn_Cancel_Click(object sender, RoutedEventArgs e)
{
ur.db.RejectChanges();
}