Лучшее решение - использовать Anonymous Types
, оно отлично работает, см. Следующее подтверждение концепции:
<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();
}
}
}