WPF - 显示"动态"表

时间:2018-01-29 19:36:04

标签: c# wpf xaml datagrid

我想在WPF中显示一个项目表。一般而言,除了:

之外,并不那么难
  • 我知道,我想在运行时显示哪些列(用户以某种方式定义它们)和
  • 我想避免使用代码隐藏:我想要显示表格的XAML位于DataTemplate内,位于另一个DataTemplate内。引入CodeBehind会有问题(但如果我没有其他选择,我会这样做。)

我虽然使用WPF' DataGrid,它足够聪明,可以在运行时提取列名(取决于集合项的属性),但我知道我想在运行时只显示哪些列 - 我必须在运行时创建具有特定属性的对象,这也是有问题的(如果可能的话)。

另外,我并不需要专门使用DataGrid,因为这只是一个简单的字符串表格,仅供预览使用 - 例如,可以在带有ItemsControl的Grid内部显示 - I只需要提供一个包含列和行的视图。

所以问题是:

  • 如何仅从ViewModel(DataContext)和XAML?或
  • 自定义DataGrid中的显示列
  • 如何仅在运行时知道列时显示预览数据的表格?

1 个答案:

答案 0 :(得分:0)

因为似乎对这个问题感兴趣并且我自己找到了解决方案,所以这里(附属性规则!)

为清楚起见,我创建了模型类来包装字符串列表列表:

public class TableDataRow
{
    public TableDataRow(List<string> cells)
    {
        Cells = cells;
    }

    public List<string> Cells { get; }
}

public class TableData
{
    public TableData(List<string> columnHeaders, List<TableDataRow> rows)
    {
        for (int i = 0; i < rows.Count; i++)
            if (rows[i].Cells.Count != columnHeaders.Count)
                throw new ArgumentException(nameof(rows));

        ColumnHeaders = columnHeaders;
        Rows = rows;
    }

    public List<string> ColumnHeaders { get; }
    public List<TableDataRow> Rows { get; }
}

现在我们定义附加属性:

public static class DataGridHelper
{
    private static void TableDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var dataGrid = d as DataGrid;
        var tableData = e.NewValue as TableData;
        if (dataGrid != null && tableData != null)
        {
            dataGrid.Columns.Clear();
            for (int i = 0; i < tableData.ColumnHeaders.Count; i++)
            {
                DataGridColumn column = new DataGridTextColumn
                {
                    Binding = new Binding($"Cells[{i}]"),
                    Header = tableData.ColumnHeaders[i]
                };
                dataGrid.Columns.Add(column);
            }

            dataGrid.ItemsSource = tableData.Rows;
        }
    }

    public static TableData GetTableData(DependencyObject obj)
    {
        return (TableData)obj.GetValue(TableDataProperty);
    }

    public static void SetTableData(DependencyObject obj, TableData value)
    {
        obj.SetValue(TableDataProperty, value);
    }

    // Using a DependencyProperty as the backing store for TableData.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TableDataProperty =
        DependencyProperty.RegisterAttached("TableData", 
            typeof(TableData), 
            typeof(DataGridHelper), 
            new PropertyMetadata(null, TableDataChanged));
}

用法很简单:

(...)
xmlns:h="clr-namespace:<namespace-of-DataGridHelper>"
(...)    

<DataGrid AutoGenerateColumns="False" h:DataGridHelper.TableData="{Binding ResultData}" />

显然DataContext必须通过TableData发布ResultData。不要忘记AutoGenerateColumns,否则您将收到额外的专栏&#34; Cells&#34;。