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
I just blogged about how to dynamically create columns for a DataGrid based on a reusable model.
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();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With