0

嗨,我希望你能帮我弄清楚为什么我不能通过我的数据表单添加新项目。

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

我将我的数据绑定到 DataForm 旁边的 DataGrid 中的选定项。我能够更新现有数据,即重命名项目。但是不能添加新的。加载控件时,正在从 EF 上下文加载绑定的数据。

我想知道是否需要描述我自己的 Country 课程;我认为 EF 应该能够处理插入。

似乎我错过了一些非常基本的东西。任何想法/资源都会有所帮助。谢谢你。

代码如下 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>

我的程序在代码后面加载数据。它在控件被实例化时被调用。

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)
4

1 回答 1

0

您正在绑定到未实现 IEditableObject 的 LoadOperation.Entities 属性。要启用添加按钮,需要实现 IEditableObject。

于 2011-02-01T18:13:09.807 回答