在运行时更改 Silverlight DataForm:DataField 标签值

发布于 2024-10-11 07:48:51 字数 627 浏览 2 评论 0原文

我有一个数据表单,它绑定到 Silverlight 应用程序中视图模型中的属性,我已经使用 WCF RIA 服务创建了实体类,并且每个属性都有 DisplayName 属性,该属性显示在数据表单数据字段标签中。我需要做的是在我创建的自定义数据字段中每个标签的末尾添加一个“:”。 我需要这种情况发生的原因是因为我的页面中有一个网格,它绑定到当前对象(例如员工)的列表,并且我不希望网格标题末尾有“:”,但我也需要“:”当我尝试编辑或添加新员工时。

这就是我到目前为止所做的,但它不起作用。

public class CustomDataField : DataField
{
    public CustomDataField()
    {

    }

    public new object Label
    {
        get { return base.Label; }
        set 
        { 
            base.Label = value;
            if( value is string )
            {
                base.Label = (string)value + ":";
            }
        }
    }
}

I'm having a dataform which is binded to a property in my view-model in a Silverlight application, I've created my entity classes with WCF RIA Services and every property has the attribute of DisplayName which is shown in the dataform datafield label. what I need to do is to add a ":" at the end of every label in the custom datafields that I create.
The reason I need this to happen is because I have a grid in my page which is binded to the list of current objects (e.g. Employees) and I don't want ":" at the end of the grid headers, but I also need ":" when I'm trying to edit or add a new employee.

This is what I've done so far, but it's not working.

public class CustomDataField : DataField
{
    public CustomDataField()
    {

    }

    public new object Label
    {
        get { return base.Label; }
        set 
        { 
            base.Label = value;
            if( value is string )
            {
                base.Label = (string)value + ":";
            }
        }
    }
}

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

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

发布评论

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

评论(1

天冷不及心凉 2024-10-18 07:48:51

(1)

当您不让 DataForm 自动生成字段时,您可以更好地控制字段并可以手动设置标签:

<tkt:DataForm AutoGenerateFields="False" AutoEdit="True">
    <tkt:DataForm.EditTemplate>
        <DataTemplate>
            <StackPanel>
                <tkt:DataField Label="SomeLabel:">
                    <TextBox Text="{Binding SomeProperty, Mode=TwoWay}" />
                </tkt:DataField>

                [...]

            </StackPanel>
        </DataTemplate>
    </tkt:DataForm.EditTemplate>
</tkt:DataForm>

(2)

如果您需要自动生成功能,但还需要更多地控制字段的生成方式显示后,您可以将 DataForm 包装到您自己的自定义控件中。您必须自己实现自动生成来构建您自己的 EditTemplate,并将其分配给 DataForm。这是我走过的路。

(3)

另一种快速但肮脏的方法是在 DataForm 呈现后迭代可视化树以更改标签。在工具包的帮助下,这一切变得非常简单:

// needs System.Windows.Controls.Toolkit.dll

using System.Linq;
using System.Windows.Controls.Primitives;

foreach (var field in myDataForm.GetVisualDescendents().OfType<DataField>())
{
    field.Label = field.Label + ":";
}

(4)

最后,我刚刚看到 DataForm 上有一个可以工作的 AutoGenerateField 事件(未经测试):

myDataForm.AutoGeneratingField += (sender, e) => e.Field.Label = e.Field.Label + ":";

(1)

When you don't let the DataForm autogenerate the fields, you have more control over the fields and can set the labels manually:

<tkt:DataForm AutoGenerateFields="False" AutoEdit="True">
    <tkt:DataForm.EditTemplate>
        <DataTemplate>
            <StackPanel>
                <tkt:DataField Label="SomeLabel:">
                    <TextBox Text="{Binding SomeProperty, Mode=TwoWay}" />
                </tkt:DataField>

                [...]

            </StackPanel>
        </DataTemplate>
    </tkt:DataForm.EditTemplate>
</tkt:DataForm>

(2)

If you need the auto-generating functionality, but you also need more control over how fields are displayed, you could wrap the DataForm into your own custom control. You'll have to implement the auto-generation yourself to build your own EditTemplate, which you'd assign to the DataForm. This is the road that I took.

(3)

Another quick and dirty way would be to iterate through the visual tree after the DataForm has rendered to change the labels. That goes pretty straightforward with a little help from the toolkit:

// needs System.Windows.Controls.Toolkit.dll

using System.Linq;
using System.Windows.Controls.Primitives;

foreach (var field in myDataForm.GetVisualDescendents().OfType<DataField>())
{
    field.Label = field.Label + ":";
}

(4)

Finally, I just saw that there is an AutoGeneratingField event on the DataForm that could work (untested):

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