4

저는 한동안 해결책을 찾기 위해 노력해 왔으며 조금 도움이되었습니다. 전에 예를 보았지만 오늘 밤에는 내가 필요한 것에 가까운 것을 찾을 수 없습니다.Silverlight 4 DataForm 재정의 변환기에 바인딩 된 콤보 상자 삽입 자동 생성

캐시 또는 DomainService에서 내 모든 DropDownLists를 제공하는 서비스가 있습니다. 이들은 IEnumerable로 표시되며 GetLookup (LookupId)이있는 저장소에서 요청됩니다. 내가 AutoGenerateFields로 설정 사용자 지정 데이터 양식을 작성하고 난이 자동 생성 필드를 차단하고

[Lookup(Lookup.Products)] 
public Guid ProductId 

:

나는 정의를 만든

내가이 같은 형태의 내 MetaDataClass를 장식 한 속성입니다.

내 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

이렇게하면 자동 생성 된 특정 컨트롤을 무시할 수 있지만 다른 컨트롤은 무시할 수 있습니까? 'for' 루프에서 정확히 반복하는 것은 무엇입니까? –

답변

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