Silverlight 4 Transform DataForm Autogenerate to Insert Combo Boxes Related Transducers

I have been working on a solution for some time and can help a bit. I know that I have seen an example of this before, but today I cannot find anything close to what I need.

I have a service that provides me all of my DropDownLists, either from the cache or from the DomainService. They are represented as IEnumerable and are requested from the repository using GetLookup (LookupId).

I created a custom attribute that I decorated with my MetaDataClass, which looks something like this:

[Lookup(Lookup.Products)] public Guid ProductId 

I created a custom data form that is set to AutoGenerateFields and I intercept the auto-generation fields.

I check my CustomAttribute attribute and it works.

Given this code in my CustomDataForm (standard comments are removed for brevity), what is the next step to override field generation and place combobox bindings on it?

 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 Value // Set the binding two way } } this.fields[e.PropertyName] = e.Field; base.OnAutoGeneratingField(e); } } 

Any given working examples for SL4 / VS2010 will be appreciated.

thanks

The update is where I am. Now I get a combo, but it is always empty, although 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; } 
+4
source share
1 answer

I have 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; } 
+4
source

Source: https://habr.com/ru/post/1305721/


All Articles