我一直在努力争取一段时间的解决方案,并可以使用一个小的帮助。我知道我以前见过一个这样的例子,但今晚我找不到任何接近我所需要的。

我提供我的一切我DropDownLists,无论是从缓存还是从的DomainService的服务。它们被呈现为IEnumerable,并从存储库被要求与GetLookup(lookupId所)。

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

[Lookup(Lookup.Products)]
public Guid ProductId

我已创建自定义数据表单被设置为AutoGenerateFields和我拦截自动生成字段。

我检查我的CustomAttribute和作品。

鉴于这种代码在我CustomDataForm(标准注释为简洁起见删除),什么是下一步覆盖场生成并放置一个绑定组合框中在它的位置?

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;                    
}
有帮助吗?

解决方案

我发现了一个解决方案。

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;                    
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top