I want to use DataTemplates that include binding markup, but I am having problems doing this in AgDataGrid.
I put together this sample that illustrates the problem. In the sample, I have both types of DataGrid control (Silverlight Standard and AgDataGrid). Each bind to a list of employees and each are supposed to display the employee Name within a TextBox (within each row). The standard Silverlight grid works. AgDataGrid does not. Do you see what mistake I have made?
Thanks, RC
********************* Page.xaml: ******************
<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightApplication1.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:my="clr-namespace:DevExpress.Windows.Controls;assembly=DevExpress.AgDataGrid.v8.2"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"/>
<RowDefinition Height="0.5*"/>
</Grid.RowDefinitions>
<my:AgDataGrid x:Name="agDataGrid" Grid.RowSpan="1" />
<data:DataGrid x:Name="standardDataGrid" Grid.RowSpan="1" Grid.Row="1" />
</Grid>
</UserControl>
******************** Page.xaml.cs: *************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DevExpress.Windows.Controls;
using System.Text;
using System.Windows.Markup;
namespace SilverlightApplication1
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
// get data
var employees = this.GetEmployees();
// bind it to both grids
this.standardDataGrid.ItemsSource = employees;
this.standardDataGrid.AutoGenerateColumns = false;
this.agDataGrid.DataSource = employees;
this.agDataGrid.AutoGenerateColumns = false;
// create a template column for each grid
var standardColumn = new DataGridTemplateColumn();
var agColumn = new AgDataGridColumn();
// Create a DataTemplate
var sb = new StringBuilder();
sb.Append("<DataTemplate ");
sb.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' >");
sb.Append("<TextBox Text='{Binding Name}' />");
sb.Append("</DataTemplate>");
var template = (DataTemplate)XamlReader.Load(sb.ToString());
// Add it to the standard and ag columns we just created.
standardColumn.CellTemplate = template;
agColumn.CellDisplayTemplate = template;
// Add the columns to thier respective grids.
this.standardDataGrid.Columns.Add(standardColumn);
this.agDataGrid.Columns.Add(agColumn);
}
private List<Employee> GetEmployees()
{
var employees = new List<Employee>();
employees.Add(new Employee() { ID = 2, Name = "John Doe", DOB = DateTime.Now });
employees.Add(new Employee() { ID = 3, Name = "Jane Doe", DOB = DateTime.Now });
return employees;
}
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime DOB { get; set; }
}
}