我一直在努力寻求解决方案,并且可以使用一些帮助。我知道我之前见过这样的例子,但今晚我找不到任何接近我需要的东西。
我有一个服务,可以从Cache或DomainService为我提供所有DropDownLists。它们以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;
}
答案 0 :(得分:4)
我找到了解决方案。
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;
}