Silverlight 4 覆盖 DataForm Autogenerate 以插入绑定到转换器的组合框 [英] Silverlight 4 Overriding the DataForm Autogenerate to insert Combo Boxes bound to Converters

查看:20
本文介绍了Silverlight 4 覆盖 DataForm Autogenerate 以插入绑定到转换器的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在努力寻求解决方案,可以提供一些帮助.我知道我以前见过这样的例子,但今晚我找不到任何接近我需要的东西.

I've been working towards a solution for some time and could use a little help. I know I've seen an example of this before, but tonight I cannot find anything close to what I need.

我有一项服务,可以从缓存或域服务中提供我所有的 DropDownList.它们显示为 IEnumerable,并通过 GetLookup(LookupId) 从存储库中请求.

I have a service that provides me all my DropDownLists, either from Cache or from the DomainService. They are presented as IEnumerable, and are requested from the a repository with GetLookup(LookupId).

我创建了一个自定义属性,我装饰了我的 MetaDataClass,它看起来像这样:

I have created a custom attribute that I have decorated my MetaDataClass that looks something like this:

[Lookup(Lookup.Products)]
public Guid ProductId

我创建了一个设置为 AutoGenerateFields 的自定义数据表单,我正在拦截自动生成字段.

I have created a custom Data Form that is set to AutoGenerateFields and I am intercepting the autogenerate fields.

我正在检查我的 CustomAttribute 并且它有效.

I am checking for my CustomAttribute and that works.

鉴于我的 CustomDataForm 中的这段代码(为简洁起见删除了标准注释),下一步要覆盖字段生成并在其位置放置绑定组合框?

Given this code in my CustomDataForm (standard comments removed for brevity), what is the next step to override the field generation and place a bound combobox in its place?

public class CustomDataForm : DataForm
{
    private Dictionary<string, DataField> fields = new Dictionary<string, DataField>();

    public Dictionary<string, DataField> Fields
    {
        get { return this.fields; }
    }

    protected override void OnAutoGeneratingField(DataFormAutoGeneratingFieldEventArgs e)
    {
        PropertyInfo propertyInfo = this.CurrentItem.GetType().GetProperty(e.PropertyName);

        foreach (Attribute attribute in propertyInfo.GetCustomAttributes(true))
        {
            LookupFieldAttribute lookupFieldAttribute = attribute as LookupFieldAttribute;
            if (lookupFieldAttribute != null)
            {                    
                //   Create a combo box.
                //   Bind it to my Lookup IEnumerable
                //   Set the selected item to my Field's Value
                //   Set the binding two way
            }
        }
        this.fields[e.PropertyName] = e.Field;
        base.OnAutoGeneratingField(e);
    }
}

任何引用的 SL4/VS2010 工作示例将不胜感激.

Any cited working examples for SL4/VS2010 would be appreciated.

谢谢

更新 - 这就是我所在的位置.我现在得到了我的组合,但它总是空的,即使 itemsSource 不是.

Update - Here's where I am at. I get my combo now, but it's always empty even though itemsSource is not.

if (lookupFieldAttribute != null)
{
    ComboBox comboBox = new ComboBox();
    Binding newBinding = e.Field.Content.GetBindingExpression(TextBox.TextProperty).ParentBinding.CreateCopy();
    newBinding.Mode = BindingMode.TwoWay;
    newBinding.Converter = new LookupConverter(lookupRepository);
    newBinding.ConverterParameter = lookupFieldAttribute.Lookup.ToString();
    comboBox.SetBinding(ComboBox.SelectedItemProperty,newBinding);
    comboBox.ItemsSource = lookupRepository.GetLookup(lookupFieldAttribute.Lookup);                    
    e.Field.Content = comboBox;                    
}

推荐答案

我找到了解决方案.

if (lookupFieldAttribute != null)
{
    ComboBox comboBox = new ComboBox();
    Binding newBinding = e.Field.Content.GetBindingExpression(TextBox.TextProperty).ParentBinding.CreateCopy();
    var itemsSource = lookupRepository.GetLookup(lookupFieldAttribute.Lookup);
    var itemsSourceBinding = new Binding { Source = itemsSource };
    comboBox.SetBinding(ItemsControl.ItemsSourceProperty, itemsSourceBinding);
    newBinding.Mode = BindingMode.TwoWay;
    newBinding.Converter = new LookupConverter(lookupRepository);
    newBinding.ConverterParameter = lookupFieldAttribute.Lookup.ToString();
    comboBox.SetBinding(ComboBox.SelectedItemProperty,newBinding);
    e.Field.Content = comboBox;                    
}

这篇关于Silverlight 4 覆盖 DataForm Autogenerate 以插入绑定到转换器的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆