【问题标题】:Why does a DataGridComboBoxColumn yield no results wheras a DataGridTemplateColumn with a ComboBox does?为什么 DataGridComboBoxColumn 没有结果,而带有 ComboBox 的 DataGridTemplateColumn 却没有结果?
【发布时间】:2014-08-09 08:40:53
【问题描述】:

如果我在 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 >

产生以下结果:

那么,我怎样才能让它们正常工作,最好是使用自动生成,这样我就不需要生成数百个 DataGrid 定义?

【问题讨论】:

  • 不知道如何让它工作?

标签: c# wpf datagrid datagridtemplatecolumn datagridcomboboxcolumn


【解决方案1】:

在寻找答案时,我偶然发现了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 的自动生成事件:

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

我敢肯定,这不是最好的方法,如果有任何有用的建议,我将不胜感激。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    相关资源
    最近更新 更多