使用 Silverlight DataForm 和 .Net RIA Services DomainDataSource 时如何设置初始值?

发布于 2024-08-04 10:53:53 字数 385 浏览 2 评论 0原文

我正在尝试 .Net RIA 和 Silverlight,我有一些相关的实体;客户、项目和工作,一个客户有很多项目,一个项目有很多工作。

在 Silverlight 应用程序中,我使用 DomainDataSource 和 DataForm 控件来执行 CRUD 操作。选择客户后,会出现一个项目列表,此时用户可以为该客户添加新项目。我希望能够自动填写客户端的值,但似乎没有任何方法可以做到这一点,虽然 DataForm 控件上有一个 AddNewItem 事件,但它似乎在 DataForm 具有之前触发新对象的实例,我不确定从 DomainDataSource SubmittingChanges 事件中搜索 ChangeSet 是执行此操作的最佳方法。

我认为这将是一个明显的功能...有人知道实现此功能的最佳方法吗?

I'm experimenting with .Net RIA and Silverlight, I have a few of related entities; Client, Project and Job, a Client has many Projects, and a Project has many Jobs.

In the Silverlight app, I'm uisng a DomainDataSource, and DataForm controls to perform the CRUD operations. When a Client is selected a list of projects appears, at which point the user can add a new project for that client. I'd like to be able to fill in the value for client automatically, but there doesn't seem to be any way to do that, while there is an AddingNewItem event on the DataForm control, it seems to fire before the DataForm has an instance of the new object and I'm not sure trawling through the ChangeSet from the DomainDataSource SubmittingChanges event is the best way to do this.

I would of thought this would of been an obvious feature... anyone know the best way to achieve this functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

甜`诱少女 2024-08-11 10:53:53

好吧,聚会迟到了,但面对同样的问题,我使用值转换器实现了一个解决方法:

public class MissingDateTimeValueConverter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        if (value is DateTime && (DateTime)value == DateTime.MinValue) {
            DateTime returnValue = DateTime.Now.Date;
            int addDays;
            if (!string.IsNullOrEmpty(parameter as string) && int.TryParse(parameter as string, out addDays)) {
                returnValue = returnValue.AddDays(addDays);
            }
            return returnValue;
        } else {
            return value;   
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        return value;            
    }

}

它将缺失的日期值(例如 01.01.0001)转换为今天的日期,并允许使用参数添加/减去天数-参数。

Well, being late for the party but facing the same issue I implemented a workaround using a value converter:

public class MissingDateTimeValueConverter : IValueConverter {

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        if (value is DateTime && (DateTime)value == DateTime.MinValue) {
            DateTime returnValue = DateTime.Now.Date;
            int addDays;
            if (!string.IsNullOrEmpty(parameter as string) && int.TryParse(parameter as string, out addDays)) {
                returnValue = returnValue.AddDays(addDays);
            }
            return returnValue;
        } else {
            return value;   
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        return value;            
    }

}

It translates missing date values (e.g. 01.01.0001) to today's date and allows adding/subtracting days using the parameter-parameter.

苏大泽ㄣ 2024-08-11 10:53:53

通常做的事情是拥有一个包含显示现有数据的 DataGrid 的屏幕。然后有一个添加按钮,它将:

  1. 创建一个新项目
  2. 创建一个 ChildWindow,将新项目传递给 ChildWindow 构造函数
  3. 在 ChildWindow 内有一个 DataForm,绑定到指定的项目

Something that is commonly done is to have a screen that contains a DataGrid showing the existing data. Then have an Add button that will:

  1. Create a new item
  2. Create a ChildWindow, passing the new item to the ChildWindow constructor
  3. Have a DataForm inside the ChildWindow, bound to the item specified
杯别 2024-08-11 10:53:53

为了在创建实体时设置默认值,我添加了一个名为 [EntityName].shared.cs 的类。然后我使用了链接中拼写的技术。这对我来说效果很好。

To set default values as my entity is created I added a class called [EntityName].shared.cs. I then used the technique spelled at this link. It worked well for me.

缘字诀 2024-08-11 10:53:53

此解决方案允许您在集合末尾添加新项目。该集合绑定到 DataForm。在退出当前处理程序之前,将 DataForm.CurrentIndex 设置为集合中的最后一项,然后取消添加过程。新项目已初始化/添加并在数据表单中可见,可供编辑。

private void ResolutionDataForm_AddingNewItem(object sender, DataFormAddingNewItemEventArgs e)
    {
        // add a new iten in collection
        Resolution resolution = new Resolution() { FaultName = "test" };
        context.Resolutions.Add(resolution);
        //through binding the form gets updated
        ResolutionDataForm.CurrentIndex = context.Resolutions.Count-1;

        // cancel de current adding procedure
        e.Cancel = true;

    }

This solution allows you to add a new item at the end of a collection. The collection is bound to DataForm. Before exiting the current handler, set the DataForm.CurrentIndex as the last item in collection then cancel the adding procedure. The new item is initialized/added and visible in Dataform ready for editing.

private void ResolutionDataForm_AddingNewItem(object sender, DataFormAddingNewItemEventArgs e)
    {
        // add a new iten in collection
        Resolution resolution = new Resolution() { FaultName = "test" };
        context.Resolutions.Add(resolution);
        //through binding the form gets updated
        ResolutionDataForm.CurrentIndex = context.Resolutions.Count-1;

        // cancel de current adding procedure
        e.Cancel = true;

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