silverlight-4.0 - Silverlight 4 覆盖 DataForm Autogenerate 以插入绑定(bind)到转换器的组合框

标签 silverlight-4.0 wcf-ria-services silverlight-toolkit dataform

一段时间以来,我一直在努力寻找解决方案,并且可以提供一些帮助。我知道我以前见过这样的例子,但今晚我找不到任何接近我需要的东西。

我有一项服务可以为我提供所有 DropDownLists,无论是来自缓存还是来自 DomainService。它们以 IEnumerable 的形式呈现,并使用 GetLookup(LookupId) 从存储库中请求。

我创建了一个自定义属性,我装饰了我的 MetaDataClass,它看起来像这样:

[Lookup(Lookup.Products)]
public Guid ProductId

我创建了一个设置为 AutoGenerateFields 的自定义数据表单,并且我正在拦截自动生成字段。

我正在检查我的 CustomAttribute 并且有效。

鉴于我的 CustomDataForm 中的这段代码(为简洁起见删除了标准注释),下一步要覆盖字段生成并在其位置放置一个绑定(bind)的组合框?
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;                    
}

最佳答案

我找到了解决方案。

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

关于silverlight-4.0 - Silverlight 4 覆盖 DataForm Autogenerate 以插入绑定(bind)到转换器的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2557951/

相关文章:

c# - 银光图表

silverlight - Silverlight 导航模板是否适用于浏览器耗尽?

entity-framework - WCF RIA 通用服务器端查询?

Silverlight Toolkit,二进制文件放在哪里

validation - TabControl 上的剩余验证工具提示

c# - 如何以编程方式将 System.Windows.Controls.Parameter 绑定(bind)到 Silverlight 中的 ComboBox 的 SelectedItem

vb.net - .Net Silverlight WCF RIA 查询项目计数

c# - RIA 服务中的异常处理

mvvm - 如何在 TreeView Silverlight 4.0 中创建 DataTemplate