views:

1235

answers:

4

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.

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).

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

[Lookup(Lookup.Products)] public Guid ProductId

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

I am checking for my CustomAttribute and that works.

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

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

Thanks

Update - Here's where I am at. I get my combo now, but its 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;                    
            }

Solution below:

By request, I am adding what finally worked:

            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;                    
            }
+1  A: 

I found a solution.

kmacmahon
+1  A: 

I would enjoy seeing your solution!

Jon Atkins
A: 

Can you share the solution you came up with?

Rick Arthur
he did, it's at the end of his post
thepaulpage
A: 

Yes do care to share the solution?

jgraygi

related questions