开发者

Silverlight 4 Overriding the DataForm Autogenerate to insert Combo Boxes bound to Converters

开发者 https://www.devze.com 2022-12-25 13:24 出处:网络
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'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 cr开发者_如何学编程eated 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 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;                    
}


I found a solution.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号