WPF 数据网格绑定

发布于 2024-10-28 21:39:10 字数 168 浏览 9 评论 0原文

我正在使用 WPF 工具包数据网格,并且过去总是为网格创建要绑定的实体,例如具有名称、地址等的联系人实体。 在我正在开发的当前应用程序上,用户可以从 50 个表中进行选择,并从表中单独选择字段来生成视图。 显然,这里绑定一个实体是行不通的,因为绑定源是动态的。

问题是我该怎么办?

谢谢

I'm using the WPF toolkit datagrid and in the past have always created entities for the grid to bind to, so for example a Contact Entity with Name, Address etc.
On the current app I'm working on the user may select from 50 tables and individually select the fields from the tables to generate a view.
Clearly here having an Entity to bind to will not work as the binding source will be dynamic.

Question is what do I do?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过去的过去 2024-11-04 21:39:10

我刚刚博客介绍了如何基于可重用模型为 DataGrid 动态创建列。

I just blogged about how to dynamically create columns for a DataGrid based on a reusable model.

眸中客 2024-11-04 21:39:10

最好的解决方案是使用匿名类型,它工作得很好,请参阅以下概念证明:

<Window x:Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:System="clr-namespace:System;assembly=mscorlib" 
    Title="MainWindow"
    Height="136" Width="525"
    Loaded="OnWindowLoaded">
<DataGrid ItemsSource="{Binding}">

</DataGrid>

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

namespace MyProject {
    public partial class MainWindow : Window
    {
        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;

        public MainWindow()
        {
            InitializeComponent();
        }

        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 = "..."},
                    };

            DataContext =
                (from i in _empCollection
                select new {i.Code, i.Name, i.Address}).ToList();
        }
    }
}

The best solution is to use Anonymous Types it works perfectly, see the following proof of concept:

<Window x:Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:System="clr-namespace:System;assembly=mscorlib" 
    Title="MainWindow"
    Height="136" Width="525"
    Loaded="OnWindowLoaded">
<DataGrid ItemsSource="{Binding}">

</DataGrid>

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

namespace MyProject {
    public partial class MainWindow : Window
    {
        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;

        public MainWindow()
        {
            InitializeComponent();
        }

        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 = "..."},
                    };

            DataContext =
                (from i in _empCollection
                select new {i.Code, i.Name, i.Address}).ToList();
        }
    }
}
始终不够爱げ你 2024-11-04 21:39:10

一种方法是创建对象集合,并为每个对象提供一个自定义 TypeDescriptor

当网格自动生成列时,它会使用对您的类(例如,Customer)的反射,并发现其属性(例如,FirstName、Balance 等)。

但这并不完全正确。 WPF 本身并不完成这项工作——它需要一个 TypeDescriptor。您还可以实现自己的 TypeDescriptor,这样您就可以假装拥有实际不存在的属性。或者就您的情况而言,假装拥有确实存在的属性。

One approach would be to create a collection of objects, and give each object a custom TypeDescriptor.

When the grid is auto generating columns, it uses reflection over your class - e.g., Customer, and discovers its properties - e.g., FirstName, Balance, etc.

But that's not entirely true. WPF doesn't do the work itself - it asks a TypeDescriptor. And you can implement your own TypeDescriptor, so you can pretend to have properties that don't actually exist. Or in your case, pretend not to have properties that do exist.

笛声青案梦长安 2024-11-04 21:39:10

您可以保留绑定源不变,但是您可以根据用户对他/她需要隐藏或查看的内容的偏好来过滤 DataGrid 的列。

You can leave binding source as it is, however you can filter DataGrid's columns based on user's preferences of what he/she needs to hide or see.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文