SL EF - 通过 Toolkit DataForm 添加新记录时遇到问题
你好 我希望您能帮助我弄清楚为什么我无法通过数据表单添加新项目。
在我的数据表单中,我定义了 Edit 和 NewItemTemplate。我正在显示正确的命令按钮,并且“+”添加按钮显示在我的表单上。但是,它总是呈灰色,不允许我添加新项目。
我将数据绑定到 DataForm 旁边的 DataGrid 中的选定项目。我能够更新现有数据,即重命名该项目。但无法添加新的。加载控件时,将从 EF 上下文加载绑定数据。
我想知道是否需要描述我自己的国家/地区课程;我认为 EF 应该能够处理插入。
看来我错过了一些非常基本的东西。任何想法/资源都会有帮助。谢谢。
代码如下 我的 DataForm XAML:
<dataFormToolkit:DataForm x:Name="dfCountry"
CurrentItem="{Binding SelectedItem, ElementName=dgCountry, Mode=TwoWay}"
CommitButtonContent="Save"
CancelButtonContent="Cancel"
AutoEdit="False"
ItemsSource="{Binding Mode=OneWay}"
AutoCommit="True"
LabelPosition="Top"
CommandButtonsVisibility="Edit, Add, Commit, Cancel, Delete"
BeginningEdit="dfCountry_BeginningEdit"
EditEnded="dfCountry_EditEnded"
DeletingItem="dfCountry_DeletingItem"
AddingNewItem="dfCountry_AddingNewItem">
<tk:DataForm.EditTemplate>
<DataTemplate>
<tk:DataField Label="Country">
<TextBox Text="{Binding Path=Name, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
</tk:DataField>
</DataTemplate>
</tk:DataForm.EditTemplate>
<tk:DataForm.NewItemTemplate>
<DataTemplate >
<tk:DataField Label="Country" >
<TextBox Text="{Binding Path=CountryName, Mode=TwoWay, ValidatesOnDataErrors=True,ValidatesOnNotifyDataErrors=True}"
HorizontalAlignment="Stretch" VerticalAlignment="Top" />
</tk:DataField>
</DataTemplate>
</tk:DataForm.NewItemTemplate>
</dataFormToolkit:DataForm>
我在后面的代码中加载数据的过程。当控件被实例化时调用它。
private void LoadData()
{
EntityQuery<Country> qry = ctx.GetCountriesQuery();
LoadOperation<Country> loadOp = ctx.Load(qry);
loadOp.Completed += new EventHandler(loadOp_Completed);
}
void loadOp_Completed(object sender, EventArgs e)
{
LoadOperation<Country> CountryDataLoadResult = (LoadOperation<Country>) sender;
dgCountry.ItemsSource = CountryDataLoadResult.Entities;
}
Country 模型在我的元数据类中声明。
[MetadataTypeAttribute(typeof(Country.CountryMetadata))]
public partial class Country
{
internal sealed class CountryMetadata
{
private CountryMetadata(){}
public int CountryID { get; set; }
public Nullable<int> CreatedBy { get; set; }
public Nullable<DateTime> CreatedDate { get; set; }
public Nullable<bool> FlagDeleted { get; set; }
public Nullable<int> ModifiedBy { get; set; }
public Nullable<DateTime> ModifiedDate { get; set; }
public string Name { get; set; }
public EntityCollection<Province> Provinces { get; set; }
}
}
在我的服务类别中,我有:
public IQueryable<Country> GetCountries()
public void InsertCountry(Country country)
public void UpdateCountry(Country currentCountry)
public void DeleteCountry(Country country)
Hi
I'm hoping you can help me figure out why I can't add new items via my dataform.
In my dataform I have defined an Edit and NewItemTemplate. I am showing the proper command buttons and the '+' add button shows up on my form. However, it's always grayed out and will not let me add a new item.
I'm binding my data to the selected item in a DataGrid beside the DataForm. I'm able to update existing data, i.e. rename the item. But can't add new ones. The binded data is being loaded from EF context when the control is loaded.
I'm wondering if I need to describe my own Country class; I figure EF should be able to handle the inserts.
It seems like I'm missing something very fundamental. Any ideas/resources would be helpful. Thank you.
Code as follows
My XAML for the DataForm:
<dataFormToolkit:DataForm x:Name="dfCountry"
CurrentItem="{Binding SelectedItem, ElementName=dgCountry, Mode=TwoWay}"
CommitButtonContent="Save"
CancelButtonContent="Cancel"
AutoEdit="False"
ItemsSource="{Binding Mode=OneWay}"
AutoCommit="True"
LabelPosition="Top"
CommandButtonsVisibility="Edit, Add, Commit, Cancel, Delete"
BeginningEdit="dfCountry_BeginningEdit"
EditEnded="dfCountry_EditEnded"
DeletingItem="dfCountry_DeletingItem"
AddingNewItem="dfCountry_AddingNewItem">
<tk:DataForm.EditTemplate>
<DataTemplate>
<tk:DataField Label="Country">
<TextBox Text="{Binding Path=Name, Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
</tk:DataField>
</DataTemplate>
</tk:DataForm.EditTemplate>
<tk:DataForm.NewItemTemplate>
<DataTemplate >
<tk:DataField Label="Country" >
<TextBox Text="{Binding Path=CountryName, Mode=TwoWay, ValidatesOnDataErrors=True,ValidatesOnNotifyDataErrors=True}"
HorizontalAlignment="Stretch" VerticalAlignment="Top" />
</tk:DataField>
</DataTemplate>
</tk:DataForm.NewItemTemplate>
</dataFormToolkit:DataForm>
My procedure to load data in the code behind. It is called when the control is instantiated.
private void LoadData()
{
EntityQuery<Country> qry = ctx.GetCountriesQuery();
LoadOperation<Country> loadOp = ctx.Load(qry);
loadOp.Completed += new EventHandler(loadOp_Completed);
}
void loadOp_Completed(object sender, EventArgs e)
{
LoadOperation<Country> CountryDataLoadResult = (LoadOperation<Country>) sender;
dgCountry.ItemsSource = CountryDataLoadResult.Entities;
}
The Country model is declared in my metadata class.
[MetadataTypeAttribute(typeof(Country.CountryMetadata))]
public partial class Country
{
internal sealed class CountryMetadata
{
private CountryMetadata(){}
public int CountryID { get; set; }
public Nullable<int> CreatedBy { get; set; }
public Nullable<DateTime> CreatedDate { get; set; }
public Nullable<bool> FlagDeleted { get; set; }
public Nullable<int> ModifiedBy { get; set; }
public Nullable<DateTime> ModifiedDate { get; set; }
public string Name { get; set; }
public EntityCollection<Province> Provinces { get; set; }
}
}
In my service class I have:
public IQueryable<Country> GetCountries()
public void InsertCountry(Country country)
public void UpdateCountry(Country currentCountry)
public void DeleteCountry(Country country)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在绑定到 LoadOperation.Entities 属性,该属性不实现 IEditableObject。要启用添加按钮,需要实现 IEditableObject。
You are binding to the LoadOperation.Entities property, which does not implement IEditableObject. Implementing IEditableObject is required for the add button to be enabled.