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

How to set GridViewSelectColumn checkbox IsEnabled

3 Answers 496 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Todd Davis
Top achievements
Rank 1
Todd Davis asked on 22 Mar 2010, 07:38 PM
I have a GridViewSelectColumn in my grid. I need to set the IsEnabled value of the checkbox based on a boolean value in the row data. So something like
myCheckBox.IsEnabled = myData.IsChecked

At first I thought I could just bind the column, but it doesn't work that way, you can't bind a column to row data, i.e.

<grid:GridViewSelectColumn IsEnabled="{Binding IsChecked}" />

My next thought was to use the RowLoaded event. This might be the right way to go, but it doesn't seem to be loading every time the data is refreshed, and that's important, as we refresh often.

        private void WorklistGrid_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
        {
            StudyData data = e.DataElement as StudyData;
            GridViewRow row = e.Row as GridViewRow;

            if (data == null || row == null)
            {
                return;
            }
            
            var cell = e.Row.Cells[0];
            var checkbox = cell.ChildrenOfType<CheckBox>();
            checkbox[0].IsEnabled = data.IsChecked;

        }

Maybe I'm just doing this wrong? Any suggestions?

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 24 Mar 2010, 09:50 AM
Hello,

The easiest way will be to create your own column and apply desired logic - in this case bind IsEnabled property of the CheckBox:

XAML
<UserControl x:Class="SilverlightApplication1.MainPage"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
    xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
    xmlns:local="clr-namespace:SilverlightApplication1">
    <Grid x:Name="LayoutRoot">
        <telerikGrid:RadGridView x:Name="RadGridView1" ItemsSource="{Binding}">
             <telerikGrid:RadGridView.Columns>
                <local:MyGridViewSelectColumn />
            </telerikGrid:RadGridView.Columns>
        </telerikGrid:RadGridView>
    </Grid>
</UserControl>


C#
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 System.Windows.Data;
using Telerik.Windows.Controls;
using System.Collections.ObjectModel;
using Telerik.Windows.Data;
using Telerik.Windows.Controls.GridView;
using System.Text;
using System.IO;
using System.ComponentModel;
 
namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            DataContext = (from i in Enumerable.Range(0, 100)
                           select new MyObject() { Property1 = i, Property2 = false }).ToList();
        }
    }
 
 
    public class MyObject : INotifyPropertyChanged
    {
        int _Property1;
        public int Property1 { get { return _Property1; } set { if (_Property1 != value) { _Property1 = value; OnPropertyChanged("Property1"); } } }
 
        bool _Property2;
        public bool Property2 { get { return _Property2; } set { if (_Property2 != value) { _Property2 = value; OnPropertyChanged("Property2"); } } }
 
        #region INotifyPropertyChanged Members
 
 
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        #endregion
    }
 
    public class MyGridViewSelectColumn : GridViewSelectColumn
    {
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            var element = base.CreateCellElement(cell, dataItem);
 
            var checkBox = element as CheckBox;
            if (checkBox != null)
            {
                checkBox.SetBinding(CheckBox.IsEnabledProperty, new Binding("Property2") { Source = dataItem });
            }
 
            return element;
        }
    }
}


You can check also the attached application.

Best wishes,
Vlad
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Todd
Top achievements
Rank 1
answered on 23 May 2011, 08:21 PM
I have run the sample you have provided and made a modification such that the check-box is collapsed if not enabled.  I also changed the test data such that every other row is disabled and thus collapsed.

However, when I scroll up and down through the grid, the visibility is inconsistent of the check-box.  For example, initially every other check box is visible, but after scrolling up and down, it seems to change randomly.

            DataContext = (from i in Enumerable.Range(0, 100)
                           select new MyObject() { Property1 = i, Property2 = (i % 2) == 0 ? true : false}).ToList();


 public class MyGridViewSelectColumn : GridViewSelectColumn
    {
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            var element = base.CreateCellElement(cell, dataItem);
 
            var checkBox = element as CheckBox;
            if (checkBox != null)
            {
                checkBox.SetBinding(CheckBox.IsEnabledProperty, new Binding("Property2") { Source = dataItem });
            }
 
            if (checkBox.IsEnabled)
            {
                checkBox.Visibility = Visibility.Collapsed;
            }
 
            return element;
        }
    }

0
Vanya Pavlova
Telerik team
answered on 24 May 2011, 10:21 AM
Hi Todd,

 

By default the RadGridView's Virtualization is turned on. Setting such binding in your custom column messes up with the virtualization, because when you are scrolling up and down the GridViewCells are being recycled. 
The cleaner solution in your case is to set CheckBox's style in RowLoaded event, as shown below:

MainPage.xaml

<telerik:RadGridView x:Name="RadGridView1" RowLoaded="RadGridView1_RowLoaded" AutoGenerateColumns="False"  ItemsSource="{Binding}">
                <telerik:RadGridView.Columns>
                <local:MyGridViewSelectColumn/>
</telerik:RadGridView.Columns>
</telerik:RadGridView>

MainPage.xaml.cs

private void RadGridView1_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
       {
          
           if (e.Row is GridViewRow && !(e.Row is GridViewNewRow))
           {
                 MyObject person = e.DataElement as MyObject;
                 var chk = e.Row.ChildrenOfType<GridViewCell>().ToList();
                 foreach (var c in chk)
                 {
                     if (person.Property2)
                     {
                         c.Visibility = Visibility.Collapsed;
                     }
                     else
                     {
                         c.Visibility = Visibility.Visible;
                     }
                 }
           
       }

GridViewSelectColumn.cs

public class MyGridViewSelectColumn : GridViewSelectColumn
    {
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            var element = base.CreateCellElement(cell, dataItem);
 
            var checkBox = element as CheckBox;
            if (checkBox != null)
            {
                checkBox.SetBinding(CheckBox.IsEnabledProperty, new Binding("Property2") { Source = dataItem });
            }
            
            return element;
        }
    }


If you need any further assistance do not hesitate to contact us!


All the best,
Vanya Pavlova
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Todd Davis
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Todd
Top achievements
Rank 1
Vanya Pavlova
Telerik team
Share this question
or