为什么DataGridComboBoxColumn没有结果,而带有ComboBox的DataGridTemplateColumn呢?

时间:2014-08-09 08:40:53

标签: c# wpf datagrid datagridtemplatecolumn datagridcomboboxcolumn

如果我在DataGrid中自动生成列,则数组和集合项不会自动成为DataGridComboBoxColumn,或者它们看起来不是......

此代码:

   <DataGrid x:Name="dataGrid" Grid.Row="1" ItemsSource="{Binding SrcCollection}" AutoGenerateColumns="False" SelectionMode="Single" 
   AlternatingRowBackground="{DynamicResource {x:Static SystemColors.GradientActiveCaptionBrushKey}}" AlternationCount="1"
   IsReadOnly="True" >
         <DataGrid.RowStyle>
            <Style>
               <Setter Property="DataGridRow.IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
            </Style>
         </DataGrid.RowStyle>
         <DataGrid.Columns>
            ...
            <DataGridComboBoxColumn ItemsSource="{Binding Path=MetadataMap}" Header="MetadataMap"  IsReadOnly="True" />
            <DataGridTemplateColumn Header="MetadataMap" IsReadOnly="True">
               <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                     <ComboBox ItemsSource="{Binding Path=MetadataMap}" SelectedIndex="0"  />
                  </DataTemplate>
               </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            ...
            <DataGridComboBoxColumn ItemsSource="{Binding Path=Rights}" Header="Rights" IsReadOnly="True" />
            <DataGridTemplateColumn Header="Rights" IsReadOnly="True">
               <DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                     <ComboBox ItemsSource="{Binding Path=Rights}" SelectedIndex="0" />
                  </DataTemplate>
               </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            ...
         </DataGrid.Columns>
      </DataGrid >

产生以下结果:

enter image description here

那么我怎样才能让它正常工作,理想情况下是自动生成,所以我不需要生成数百个DataGrid定义?

1 个答案:

答案 0 :(得分:0)

在寻找答案时,我偶然发现了this postthis one。虽然它们更多的是操纵内容,但我能够修改它,并让它做我需要的。

这是我创建的课程:

  public class MyDataGridComboBoxColumn : DataGridComboBoxColumn
  {
    public string ColumnName
    {
      get;
      set;
    }

    protected override System.Windows.FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
      var element = (dynamic)base.GenerateElement(cell, dataItem);
      ComboBox CB = new ComboBox();
      try
      {
        dynamic Value = dataItem.GetType().GetProperty(cell.Column.Header.ToString()).GetValue(dataItem, null);
        CB = new ComboBox();
        if (Value != null)
          foreach (var val in Value)
            CB.Items.Add(val);
        CB.SelectedIndex = 0;
      }
      catch { }
      return CB;
    }
  }

然后我超越了DataGrid的AutoGenerating Event:

private void AutoGenerating(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
  if (e.PropertyType.IsArray || 
    e.PropertyType.ToString().ToUpperInvariant().Contains("Dictionary".ToUpperInvariant()) ||
    e.PropertyType.ToString().ToUpperInvariant().Contains("List".ToUpperInvariant()))
  {
    MyDataGridComboBoxColumn col = new MyDataGridComboBoxColumn();
    col.ColumnName = e.PropertyName;
    e.Column = col;
    e.Column.Header = e.PropertyName;
  }
}

我敢肯定,这不是最好的方法,并且会感谢任何有用的建议。