【问题标题】:Silverlight 4 Overriding the DataForm Autogenerate to insert Combo Boxes bound to ConvertersSilverlight 4 覆盖 DataForm Autogenerate 以插入绑定到转换器的组合框
【发布时间】:2010-04-01 03:18:59
【问题描述】:

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

我有一项服务,可以从缓存或 DomainService 中为我提供所有 DropDownLists。它们以 IEnumerable 的形式呈现,并使用 GetLookup(LookupId) 从存储库请求。

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

[Lookup(Lookup.Products)]
public Guid ProductId

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

我正在检查我的 CustomAttribute 并且有效。

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

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 工作示例将不胜感激。

谢谢

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

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;                    
}

【问题讨论】:

  • 那么,这可以让您覆盖某些自动生成的控件,但不能覆盖其他控件?你到底在 for 循环中迭代了什么?

标签: silverlight-4.0 wcf-ria-services silverlight-toolkit dataform


【解决方案1】:

我找到了解决办法。

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;                    
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    相关资源
    最近更新 更多