数据绑定到具有未知列数的网格

时间:2011-04-11 03:11:51

标签: c# wpf data-binding

如何绑定到具有未知列数的WPF网格?

我有一个类可以返回列数和列名等,以及将绑定到每一行的项。基本上,我想在不使用数据表的情况下完成DataTable的功能。必须有一个我需要实现的接口或类似的东西。

3 个答案:

答案 0 :(得分:1)

如果您的对象(DataContext)实现IEnumerable,则datagrid将能够吸入对象并显示记录。只需将AutoGenerateColumns设置为true,它就会根据您传递的对象为您生成列。

答案 1 :(得分:1)

我最终通过使用SyncFusion网格控件解决了这个问题。我发现内置网格以及市场上的几乎每个网格都非常朝向一行,即从数据库返回的对象。

如果您偏离此模型,则大多数网格变得无用或至少非常难以使用。就我而言,数据库中的每一行代表一个单元格。我对所有网格的关键问题是获取WPF模板以使用未绑定的数据。 SyncFusion网格是我能找到的唯一可以同时使用这两个功能的网格(ubound数据和数据模板)。

我还发现flexgrid会解决这个问题,虽然在这方面有点难以处理。

答案 2 :(得分:-1)

更新


最佳解决方案是使用Anonymous Types。它运作完美,请参阅以下概念证明:

<Window x:Class="AnonymousTypes.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="OnWindowLoaded">
    <Grid>
        <DataGrid Name="MyDataGrid" ItemsSource="{Binding}" AutoGeneratedColumns="OnMyDataGridAutoGeneratedColumns">

        </DataGrid>
    </Grid>
</Window>

代码隐藏:

using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace AnonymousTypes
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public class Employee
        {
            public int Id { get; set; }
            public string Code { get; set; }
            public string Name { get; set; }
            public int Job { get; set; }
            public string Address { get; set; }
        }

        private ObservableCollection<Employee> _empCollection;

        private void OnWindowLoaded(object sender, RoutedEventArgs e)
        {
            // Generate test data
            _empCollection =
                new ObservableCollection<Employee>
                    {
                        new Employee {Id = 234, Code = "E041", Name = "Employee1", Job = 1, Address = "..."},
                        new Employee {Id = 245, Code = "E701", Name = "Employee2", Job = 3, Address = "..."},
                        new Employee {Id = 728, Code = "E001", Name = "Employee3", Job = 9, Address = "..."},
                        new Employee {Id = 663, Code = "E051", Name = "Employee4", Job = 7, Address = "..."},
                    };
            // Notice that here you can chose the column you want,
            // and it can be variable with each query depending on your needs
            // Just add the columns you need to the anonymous type
            DataContext =
                (from i in _empCollection
                 select new {TheCode =  i.Code, TheName = i.Name, TheAddress = i.Address }).ToList();
        }

        private void OnMyDataGridAutoGeneratedColumns(object sender, EventArgs e)
        {
            // Now you can change the column names as you need
            MyDataGrid.Columns[0].Header = "Header 1 [Code]";
            MyDataGrid.Columns[1].Header = "Header 2 [Name]";
            MyDataGrid.Columns[2].Header = "Header 3 [Address]";
        }
    }
}

您可以在使用AutoGeneratedColumns事件完成数据绑定后更改列标题。

相关问题