【问题标题】:How to include ListBox inside DataGrid control如何在 DataGrid 控件中包含 ListBox
【发布时间】:2016-01-21 11:47:43
【问题描述】:

我正在使用 MVVM 模式开发 WPF 4 应用程序。我有一个 DataGrid 控件,其中的列自动生成并与 DataTable 对象绑定。

我现在的桌子是这样的:

+------------+---------------+---------------+
| Date       | Shop 1        | Shop 2        |
+------------+---------------+---------------+
| 2016-01-01 | 09:00 - 13:00 | N/A           |
+------------+---------------+---------------+
| 2016-01-02 | N/A           | 14:00 - 18:00 |
+------------+---------------+---------------+

我需要这个结果:

+------------+---------------+---------------+
| Date       | Shop 1        | Shop 2        |
+------------+---------------+---------------+
| 2016-01-01 | 9:00 - 13:00  | N/A           |
|            | 14:00 - 18:00 |               |
+------------+---------------+---------------+
| 2016-01-02 |               | 14:00 - 18:00 |
|            | 9:00 - 10:00  |               |
|            | 12:00 - 14:00 |               |
+------------+---------------+---------------+

对于每个小时范围,我需要不同的颜色,因此我会为表格的每个单元格绑定一个列表框。

其实我的 XAML 代码是这样的:

<DataGrid x:Name="grdScheduler" 
          IsEnabled="False"
          AutoGenerateColumns="True" 
          HeadersVisibility="None" 
          ItemsSource="{Binding SchedulerDataTable, Mode=OneWay}">            
</DataGrid>

拜托,你能帮帮我吗?

谢谢

【问题讨论】:

  • 真的需要自动生成列吗?
  • Shop 1 和Shop 2 DataColumn 的数据类型是什么?它是一个数组/列表还是只是一个字符串?
  • @LawrenceA.Contreras:不幸的是,我需要自动生成列。我有一个 Shop1 和 Shop2 列的列表对象。

标签: c# wpf xaml datagrid listbox


【解决方案1】:

我认为this 与您的要求相似。但我举了一个适合你问题的例子。

您需要为您的List&lt;object&gt; 列类型提供DataGridtemplateColumn。为此,我们为 Datagrid 的 AutoGeneratingColumn 事件创建了一个事件处理程序。是的,您需要在后面编写一些代码,但这不会破坏您的 MVVM 世界中的任何内容。您可以在后面编写代码,或者如果您想要一种更简洁的方式来实现它,您可以实现自己的数据网格自定义控件。 现在在 DataGrid.Resource 中,我们有一个名为 ListTemplate 的 DataTemplate。这将是每个 List&lt;object&gt; 列的单元格模板。

<DataGrid x:Name="grdScheduler" 
      IsEnabled="False"
      AutoGenerateColumns="True" 
      HeadersVisibility="None" 
      ItemsSource="{Binding SchedulerDataTable, Mode=OneWay}"         
      AutoGeneratingColumn="grdScheduler_AutoGeneratingColumn">
  <DataGrid.Resources>
    <DataTemplate x:Key="ListTemplate">
      <ListBox ItemsSource="{Binding}" />
    </DataTemplate>
  </DataGrid.Resources>
</DataGrid>

对于List&lt;object&gt; 类型的每一列,您需要使用 DynamicDataGridTemplateColumn 并使用 ListTemplate 作为 CellTemplate。在此代码块中,您还可以根据需要为每个生成的列使用不同的模板。

    private void grdScheduler_AutoGeneratingColumn(object sender, System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs e)
    {
      if (e.PropertyType == typeof(List<object>))
      {
        DynamicDataGridTemplateColumn dgTemplateCol = new DynamicDataGridTemplateColumn();
        dgTemplateCol.CellTemplate = (sender as DataGrid).FindResource("ListTemplate") as DataTemplate;
        dgTemplateCol.ColumnName = e.PropertyName;
        e.Column = dgTemplateCol;
      }
    }

这是 DynamicDataGridTemplateColumn 的代码。

  public class DynamicDataGridTemplateColumn : DataGridTemplateColumn
  {
    public string ColumnName
    {
      get;
      set;
    }

    protected override System.Windows.FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
      // The DataGridTemplateColumn uses ContentPresenter with your DataTemplate.
      ContentPresenter cp = (ContentPresenter)base.GenerateElement(cell, dataItem);
      // Reset the Binding to the specific column. The default binding is to the DataRowView.
      BindingOperations.SetBinding(cp, ContentPresenter.ContentProperty, new Binding(this.ColumnName));
      return cp;
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2014-10-16
    • 2011-05-05
    相关资源
    最近更新 更多