是否可以将 DataField 添加到 Silverlight DataForm 而无需创建整个编辑模板?

发布于 2024-08-17 12:14:27 字数 540 浏览 8 评论 0原文

我正在将 DataForm 用于具有大约 40 个属性的实体。我对表单显示除 3 个属性之外的所有属性的方式感到满意。这 3 个属性恰好是项目列表。

我不想编写整个编辑模板,这似乎非常适得其反。

<dataFormToolkit:DataForm AutoGenerateFields="True" CurrentItem="{Binding XXX, Mode=TwoWay, Source={StaticResource XXXViewModel}}" >
                    <dataFormToolkit:DataField Label="Client"  >
                        <ListBox ItemsSource="{Binding Client}"></ListBox>
                    </dataFormToolkit:DataField>
                </dataFormToolkit:DataForm>

I am using the DataForm for an entity with about 40 attributes. I'm happy with how the form displays all but 3 of the attributes. These 3 attributes happen to be lists of items.

I don't want to have to code out an entire edit template, seems very counter productive.

<dataFormToolkit:DataForm AutoGenerateFields="True" CurrentItem="{Binding XXX, Mode=TwoWay, Source={StaticResource XXXViewModel}}" >
                    <dataFormToolkit:DataField Label="Client"  >
                        <ListBox ItemsSource="{Binding Client}"></ListBox>
                    </dataFormToolkit:DataField>
                </dataFormToolkit:DataForm>

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

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

发布评论

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

评论(3

你穿错了嫁妆 2024-08-24 12:14:27

WCF RIA 服务 包括 Silverlight 业务应用程序项目模板演示了如何创建 CustomDataForm,其中重写 OnAutoGenerateField 并仅修改所需属性的字段。我在这里复制了代码来说明这个想法,但我建议您查看真实的代码,看看他们如何使用 ReplaceTextBox 扩展方法来处理数据绑定。 下载链接

public class CustomDataForm : DataForm
{
    protected override void OnAutoGeneratingField(DataFormAutoGeneratingFieldEventArgs e)
    {
        // Get metadata about the property being defined
        PropertyInfo propertyInfo = this.CurrentItem.GetType().GetProperty(e.PropertyName);

        // Do the password field replacement if that is the case
        if (e.Field.Content is TextBox && this.IsPasswordProperty(propertyInfo))
        {
            e.Field.ReplaceTextBox(new PasswordBox(), PasswordBox.PasswordProperty);
        }

        // Keep this newly generated field accessible through the Fields property
        this.fields[e.PropertyName] = e.Field;

        // Call base implementation (which will call other event listeners)
        base.OnAutoGeneratingField(e);
    }
}

The the WCF RIA Services includes a Silverlight Business Application project template that demonstrates creating a CustomDataForm where they override OnAutoGeneratingField and modify the field for just the attributes you want. I've copied the code here for you to illustrate the idea but I'd suggest you check out the real thing to see how they are using the ReplaceTextBox extension method to deal with the Data Binding as well. Download link.

public class CustomDataForm : DataForm
{
    protected override void OnAutoGeneratingField(DataFormAutoGeneratingFieldEventArgs e)
    {
        // Get metadata about the property being defined
        PropertyInfo propertyInfo = this.CurrentItem.GetType().GetProperty(e.PropertyName);

        // Do the password field replacement if that is the case
        if (e.Field.Content is TextBox && this.IsPasswordProperty(propertyInfo))
        {
            e.Field.ReplaceTextBox(new PasswordBox(), PasswordBox.PasswordProperty);
        }

        // Keep this newly generated field accessible through the Fields property
        this.fields[e.PropertyName] = e.Field;

        // Call base implementation (which will call other event listeners)
        base.OnAutoGeneratingField(e);
    }
}
百合的盛世恋 2024-08-24 12:14:27

它会起作用:尝试一下,

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class IsPassword : System.Attribute { }

  public class CustomDataForm : DataForm
    {
        protected override void OnAutoGeneratingField(DataFormAutoGeneratingFieldEventArgs e)
        {
            // Get metadata about the property being defined
            PropertyInfo propertyInfo = this.CurrentItem.GetType().GetProperty(e.PropertyName);

            // Do the password field replacement if that is the case
            var attributes = propertyInfo.GetCustomAttributes(typeof(IsPassword), false).ToList();

            if (attributes.Any(obj=>obj is IsPassword))
            {
                PasswordBox box= new PasswordBox();
                Binding binding = new Binding(e.PropertyName);
                binding.Mode = BindingMode.TwoWay;
                box.SetBinding(PasswordBox.PasswordProperty, binding);
                e.Field.Content=box;            
            }
            base.OnAutoGeneratingField(e);
        }
    }

然后将 [IsPassword] 添加到您的属性中

It will work : try that

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
    public class IsPassword : System.Attribute { }

  public class CustomDataForm : DataForm
    {
        protected override void OnAutoGeneratingField(DataFormAutoGeneratingFieldEventArgs e)
        {
            // Get metadata about the property being defined
            PropertyInfo propertyInfo = this.CurrentItem.GetType().GetProperty(e.PropertyName);

            // Do the password field replacement if that is the case
            var attributes = propertyInfo.GetCustomAttributes(typeof(IsPassword), false).ToList();

            if (attributes.Any(obj=>obj is IsPassword))
            {
                PasswordBox box= new PasswordBox();
                Binding binding = new Binding(e.PropertyName);
                binding.Mode = BindingMode.TwoWay;
                box.SetBinding(PasswordBox.PasswordProperty, binding);
                e.Field.Content=box;            
            }
            base.OnAutoGeneratingField(e);
        }
    }

then just add [IsPassword] to your property

李不 2024-08-24 12:14:27

我很确定这是不可能的。如果我是你,我会吞下悲伤并创建该编辑模板。

我能看到的唯一选择是使用视图模型中的数据并创建一个单独的类来保存 37 个不需要更改的属性。然后,您为需要特别注意的 3 个对象创建一个单独的实体。这样您就可以拥有两种数据表单,一种是自动生成的,另一种是自定义的。希望您可以对它们进行样式设计,使它们看起来像一种形式。我知道需要做很多工作,但创建完整的编辑模板可能会需要更多工作。

I'm pretty sure it's not possible. If I were you I would swallow my grief and create that edit template.

The only alternative I can see is to work with the data in your viewmodel and create a separate class that holds the 37 properties that need no changing. Then you make a separate entity for the 3 that need special attention. This way you could have two data forms, one autogenerated and one custom. Hopefully you can then work with styling them so they look like one form. A lot of work, I know, but it might be even more work to create the full edit template.

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