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

[Solved] CheckBoxColumn

3 Answers 172 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alex
Top achievements
Rank 1
Alex asked on 23 Nov 2010, 12:32 PM
removed

3 Answers, 1 is accepted

Sort by
0
Alexey Oyun
Top achievements
Rank 1
answered on 23 Nov 2010, 12:33 PM
  Hi,

  I need to set boolean property of list item. So I have created CheckBoxColumn similar to SelectionColumn.
  It seems to work fine. But there is one problem with it.
  How do I apply header text style to my checkbox in header. So that when user changes theme, my custom header text style will change too?

  For those who interested in code.
  CheckBoxColumn:
 
 
public class CheckBoxGridColumn: GridViewColumn
    {
        private string _booleanPropertyPath;
        private object _headerContent;
        private HorizontalAlignment _contentHorizontalAlignment;
 
        public string BooleanPropertyPath
        {
            get { return _booleanPropertyPath; }
            set { _booleanPropertyPath = value; }
        }
 
        public object HeaderContent
        {
            get { return _headerContent; }
            set { _headerContent = value; }
        }
 
        public HorizontalAlignment ContentHorizontalAlignment
        {
            get { return _contentHorizontalAlignment; }
            set { _contentHorizontalAlignment = value; }
        }
 
        public CheckBoxGridColumn()
        {
            IsResizable = false;
            IsReorderable = false;
            Width = GridViewLength.Auto;
            MinWidth = 25;
        }
 
        internal bool CanEdit(object item)
        {
            return false;
        }
 
        public override bool CanFilter()
        {
            return false;
        }
 
        public override bool CanSort()
        {
            return false;
        }
 
        public override bool CanGroup()
        {
            return false;
        }
 
        public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem)
        {
            if (cell != null)
            {
                CheckBox checkBox = cell.Content as CheckBox;
                if (checkBox == null)
                {
                    checkBox = new CheckBox();
                    if (cell.Parent != null)
                    {
                        StyleManager.SetTheme(checkBox, StyleManager.GetTheme(cell.Parent));
                    }
                    checkBox.IsTabStop = false;
                    checkBox.HorizontalAlignment = ContentHorizontalAlignment;
                    checkBox.Click += rowCheckBox_Click;
                }
 
                if (dataItem != null && BooleanPropertyDefined())
                {
                    checkBox.SetBinding(ToggleButton.IsCheckedProperty, new Binding(BooleanPropertyPath)
                        {
                            Source = dataItem,
                            Mode = BindingMode.TwoWay
                        });
                }
 
                return checkBox;
            }
 
            return base.CreateCellElement(cell, dataItem);
        }
 
        private void rowCheckBox_Click(object sender, RoutedEventArgs e)
        {
            RefreshHeaderCheckbox();
        }
 
        private void RefreshHeaderCheckbox()
        {
            DataItemCollection items = DataControl.Items;
            IEnumerable itemsSource = items.SourceCollection;
            CheckBox checkBoxInHeader = GetCheckBoxInHeader();
            if (itemsSource != null && checkBoxInHeader != null && BooleanPropertyDefined())
            {
                int checkedCount = itemsSource.Cast<object>().Count(item => Convert.ToBoolean(getProperty(item, BooleanPropertyPath)));
                int totalCount = items.TotalItemCount;
                if(checkedCount == totalCount)
                {
                    checkBoxInHeader.IsChecked = true;
                }
                else if(checkedCount == 0)
              {
                    checkBoxInHeader.IsChecked = false;
              }
              else
              {
                    checkBoxInHeader.IsChecked = null;
              }
            }
        }
 
        public override object Header
        {
            get
            {
                var header = base.Header;
                var isHeaderDefined = header == null;
                if (isHeaderDefined && DataControl != null)
                {
                    var headerCheckBox = new CheckBox();
                    headerCheckBox.Click += headerCheckBox_Click;
                    headerCheckBox.Content = HeaderContent;
                    base.Header = headerCheckBox;
                    RefreshHeaderCheckbox();
                }
 
                return base.Header;
            }
            set
            {
                base.Header = value;
            }
        }
 
        void headerCheckBox_Click(object sender, RoutedEventArgs e)
        {
            var headerCheckBox = (CheckBox) sender;
 
            if (DataControl == null)
                return;
 
            CheckOrUncheckAllItems(headerCheckBox);
        }
 
        private void CheckOrUncheckAllItems(ToggleButton headerCheckBox)
        {
            if(BooleanPropertyDefined())
            {
                GridViewDataControl control = DataControl;
                foreach (object item in (IEnumerable)control.ItemsSource)
                {
                    setProperty(item, BooleanPropertyPath, headerCheckBox.IsChecked);
                }
            }
        }
 
        internal CheckBox GetCheckBoxInHeader()
        {
            return Header as CheckBox;
        }
 
        private bool BooleanPropertyDefined()
        {
            return !string.IsNullOrEmpty(BooleanPropertyPath);
        }
 
        private object getProperty(object containingObject, string propertyName)
        {
            return containingObject.GetType().InvokeMember(propertyName, BindingFlags.GetProperty, null, containingObject, null);
        }
 
        private void setProperty(object containingObject, string propertyName, object newValue)
        {
            containingObject.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, containingObject, new[] { newValue });
        }
    }


Usage

XAML:
<Controls:RadGridView x:Name="Grid" AutoGenerateColumns="False">
    <Controls:RadGridView.Columns>
        <HelperClasses:ButtonGridColumn Header="Delete"/>
        <GridColumns:CheckBoxGridColumn HeaderContent="In Use" BooleanPropertyPath="InUse" ContentHorizontalAlignment="Center" />
    </Controls:RadGridView.Columns>
</Controls:RadGridView>

Code behind:
public partial class CustomGridCell
{
    public CustomGridCell()
    {
        InitializeComponent();
    }
 
    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        List<Data> itemsSource = new List<Data>();
        for (int i = 0; i < 1000; i++ )
        {
            itemsSource.Add(new Data() {Name = "D" + i, InUse = true});
        }
            Grid.ItemsSource = itemsSource;
    }
}
 
public class Data: INotifyPropertyChanged
{
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged("Name");
        }
    }
 
    private bool _inUse;
    public bool InUse
    {
        get { return _inUse; }
        set
        {
            _inUse = value;
            OnPropertyChanged("InUse");
        }
    }
 
    #region Implementation of INotifyPropertyChanged
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    private void OnPropertyChanged(string propertyName)
    {
        if (null != PropertyChanged)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

Hope it will save some copy paste code, as it did for me.

Sincerely, Alexey.
 
0
Veselin Vasilev
Telerik team
answered on 26 Nov 2010, 02:34 PM
Hi Alexey Oyun,

Please find attached a sample project. I hope it helps.

All the best,
Veselin Vasilev
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Alexey Oyun
Top achievements
Rank 1
answered on 26 Nov 2010, 02:55 PM
Hi,

It still don't get Grid header text style. So it almost same as if I will set by hand.

Anyway, thanks for replay.

Alexey
Tags
GridView
Asked by
Alex
Top achievements
Rank 1
Answers by
Alexey Oyun
Top achievements
Rank 1
Veselin Vasilev
Telerik team
Share this question
or