2012-09-07 22 views
0

我在用戶控件(DataGridView)中有一個DataGrid。該用戶控件傳播的結合DataGrid的ItemsSource時從應用程序中的任何地方,並與K.如何在通用GridView中解決泛型ComboBox表示法

的列表填充它對於這個例子讓定義CLAS對於K具有自定義一些屬性屬性:

public class Foo 
{ 
    [Enumeration(IsEnum=true, EnumerationType=typeof(MessageType))] //MessageType is enumeration and is consisted of values: 'Error', 'Warning' and 'Info' 
    public MessageType MessageType { get; set; } 

    [Enumeration(IsEnum=true, EnumerationType=typeof(FooType))] //FooType is enumeration and is consisted of values: 'Sweet' and 'Salty' 
    public FooType FooType { get; set; } 
} 

DataGrid中有自動生成列的事件。

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
    { 
     foreach (Attribute attribute in (e.PropertyDescriptor as System.ComponentModel.PropertyDescriptor).Attributes) 
     { 
      Utilities.EnumerationAttribute enumAttribute = attribute as Utilities.EnumerationAttribute; 

      if (enumAttribute != null 
       && enumAttribute.IsEnum) 
      { 
       DataGridTemplateColumn templateColumn = new DataGridTemplateColumn(); 

       templateColumn.CellTemplate = (DataTemplate)Resources["enumTemplate"]; 

       e.Column = templateColumn; 
      } 
     } 
     e.Column.IsReadOnly = true; 
    } 

資源爲「enumTemplate」在MergedDictionary定義的DataTemplate

<DataTemplate x:Key="enumTemplate"> 
    <StackPanel> 
     <ComboBox/> 
    </StackPanel> 
</DataTemplate> 

我正打算做的是設置每個組合框的ItemsSource的網格將與Enum.GetNames的返回值生成( enumAttribute.EnumerationType),它的類型是string []。

現在在這裏有很多匿名,我不知道這些DataGrid在運行時顯示的屬性名稱,類型,甚至是對象的類型。

我有幾次嘗試到這...像定義一個屬性DataSourcesList類型List>其中NamedArray中存放項目以填充組合框和屬性的名稱(從e.PropertyName),以便我知道哪個NamedArray要使用組合框生成......是這樣的:

DataSourcesList.Add(new DataSourceWithMappedName<System.Object>() { MappedName = e.PropertyName, SourceList = Enum.GetNames(enumAttribute.EnumerationType) }); 

然後改變的DataTemplate:

<DataTemplate x:Key="enumTemplate"> 
    <DataTemplate.Resources> 
     <converters:IListToItemsSource x:Key="ilistToItemsSource"/> 
    </DataTemplate.Resources> 
    <StackPanel> 
     <ComboBox ItemsSource="{Binding DataSourcesList, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:DataGridView}}, Converter={StaticResource ilistToItemsSource}, ConverterParameter={Binding somethingButAllInVaine}}"/> 
    </StackPanel> 
</DataTemplate> 

,但這不能因爲ConverterParameter做的是不是一個DependencyObject的,不能綁定。

也許我應該說,同樣的原則應該稍後用於綁定集合而不僅僅是枚舉。

所以,請任何人都可以幫我解決一般的GridView中的通用ComboBox表示。

Tnx和快樂編碼。

回答

1

也許在代碼中設置DataTemplate會更容易。首先定義datatemplate,然後將ComboBox ItemSource Binding Source設置爲由Enum.GetNames()產生的List of String

編碼方式

 // Create template 
     DataTemplate newTemplate = new DataTemplate(); 
     FrameworkElementFactory stackFactory = new FrameworkElementFactory(typeof(StackPanel)); 
     FrameworkElementFactory comboFactory = new FrameworkElementFactory(typeof(ComboBox)); 
     Binding newBinding = new Binding(); 
     newBinding.Source = Enum.GetNames(typeof(enumAttribute.EnumerationType)); 
     comboFactory.SetBinding(ComboBox.ItemsSourceProperty, newBinding); 
     stackFactory.AppendChild(comboFactory); 
     newTemplate.VisualTree = stackFactory; 

     // Set the template 
     templateColumn.CellTemplate = newTemplate; 

XAML /編碼方式*

<CollectionViewSource x:Key="EnumCollection"/> 

<DataTemplate x:Key="enumTemplate"> 
    <StackPanel> 
     <ComboBox ItemsSource="{Binding Source={StaticResource EnumCollection}}" /> 
    </StackPanel> 
</DataTemplate> 

代碼(請確保您設置Collection Source):

CollectionViewSource enumCollection = (CollectionViewSource)this.FindResource("EnumCollection"); 
enumCollection.Source =Enum.GetNames(typeof(enumAttribute.EnumerationType)); 

純XAML中

<ObjectDataProvider x:Key="EnumCollection" 
        MethodName="GetValues" 
        ObjectType="{x:Type sys:Enum}"> 
    <ObjectDataProvider.MethodParameters> 
     <x:Type TypeName="YourEnum" /> 
    </ObjectDataProvider.MethodParameters> 
</ObjectDataProvider> 

<DataTemplate x:Key="enumTemplate"> 
    <StackPanel> 
     <ComboBox ItemsSource="{Binding Source={StaticResource EnumCollection}}" /> 
    </StackPanel> 
</DataTemplate> 
+0

的最佳解決方案。是否正確,不完全如我所願,但仍然有效。你也許也知道XAML解決方案還是有什麼想法? – nzic

+0

請參閱上面的編輯。 –