This is a migrated thread and some comments may be shown as answers.

checkbox column

1 Answer 291 Views
GridView
This is a migrated thread and some comments may be shown as answers.
hwsoderlund
Top achievements
Rank 1
hwsoderlund asked on 03 Dec 2008, 08:34 AM
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 { getset; } 
        public int Age { getset; } 
        public string Name { getset; } 
        public string UniqueID { getset; } 
        public bool IsChecked { getset; } 
 
        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; 
        } 
    } 
 

1 Answer, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 05 Dec 2008, 01:00 PM
Hi hwsoderlund,

Setting CellStyle and HeaderCellStyle properties of the GridViewDataColumn object will be available with our first official beta which is scheduled for the mid-December. You can refer to this thread in our public forum where we discuss the same problem.

P.S. Your way to add custom columns looks OK and will work fine with the beta.

Greetings,
Nedyalko Nikolov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
hwsoderlund
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Share this question
or