I'm trying to add a checkbox column, and I'm not very successful. I've tried setting both the ContentTemplate and the Template property in the CellStyle property for the column, but I can't get any custom content to display at all. Still, it doesn't crash, so I guess I'm not completely out of line. I must be missing something here, please take a look at the code below:
<UserControl x:Class="SilverlightApplication4.Page4" |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" |
xmlns:telerikbase="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" |
xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls.GridView;assembly=Telerik.Windows.Controls.GridView" |
Width="800" |
Height="600"> |
<UserControl.Resources> |
<Style x:Key="CheckBoxStyle" |
TargetType="telerikGridView:GridViewCell"> |
<Setter Property="ContentTemplate"> |
<Setter.Value> |
<DataTemplate> |
<StackPanel> |
<CheckBox IsChecked="{Binding}"></CheckBox> |
</StackPanel> |
</DataTemplate> |
</Setter.Value> |
</Setter> |
<!--<Setter Property="Template"> |
<Setter.Value> |
<ControlTemplate> |
<StackPanel Orientation="Horizontal" > |
<CheckBox></CheckBox> |
<ContentPresenter Content="{Binding}"></ContentPresenter> |
</StackPanel> |
</ControlTemplate> |
</Setter.Value> |
</Setter>--> |
</Style> |
</UserControl.Resources> |
<Grid> |
<telerik:RadGridView Name="RadGridView1" |
ColumnsWidthMode="Fill" |
AutoGenerateColumns="False"> |
</telerik:RadGridView> |
</Grid> |
</UserControl> |
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 Telerik.Windows.Controls; |
using Telerik.Windows.Controls.GridView; |
using System.Windows.Threading; |
namespace SilverlightApplication4 |
{ |
public partial class Page4 : UserControl |
{ |
List<Person> _People = new List<Person>(); |
public Page4() |
{ |
InitializeComponent(); |
InitializeGrid(); |
PopulateGrid(); |
} |
private void PopulateGrid() |
{ |
_People.Clear(); |
_People.Add(new Person(1, 27, "Pete", true)); |
_People.Add(new Person(2, 26, "Jenny", true)); |
_People.Add(new Person(3, 25, "Steve", false)); |
RadGridView1.ItemsSource = _People; |
} |
private void InitializeGrid() |
{ |
RadGridView1.Columns.Add(GetCheckBoxColumn()); |
RadGridView1.Columns.Add(GetColumn("ID", "ID")); |
RadGridView1.Columns.Add(GetColumn("Name", "Name")); |
RadGridView1.Columns.Add(GetColumn("Age", "Age")); |
} |
private GridViewDataColumn GetColumn(string header, string path) |
{ |
var col = new GridViewDataColumn(); |
col.HeaderText = header; |
col.DataMemberPath = path; |
col.UniqueName = path; |
return col; |
} |
private GridViewDataColumn GetCheckBoxColumn() |
{ |
var col = new GridViewDataColumn(); |
col.DataMemberPath = "IsChecked"; |
col.CellStyle = this.Resources["CheckBoxStyle"] as Style; |
return col; |
} |
} |
public class Person |
{ |
public int ID { get; set; } |
public int Age { get; set; } |
public string Name { get; set; } |
public string UniqueID { get; set; } |
public bool IsChecked { get; set; } |
public Person(int id, int age, string name, bool isChecked) |
{ |
this.ID = id; |
this.Age = age; |
this.Name = name; |
this.UniqueID = Guid.NewGuid().ToString(); |
this.IsChecked = isChecked; |
} |
} |
} |