SL EF - 通过 Toolkit DataForm 添加新记录时遇到问题

发布于 2024-10-07 00:54:27 字数 3249 浏览 3 评论 0原文

你好 我希望您能帮助我弄清楚为什么我无法通过数据表单添加新项目。

在我的数据表单中,我定义了 EditNewItemTemplate。我正在显示正确的命令按钮,并且“+”添加按钮显示在我的表单上。但是,它总是呈灰色,不允许我添加新项目。

我将数据绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甩你一脸翔 2024-10-14 00:54:28

您正在绑定到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文