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

非 Y 不嫁゛ 提交于 2019-12-12 12:14:12

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/2557951/silverlight-4-overriding-the-dataform-autogenerate-to-insert-combo-boxes-bound-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!