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

Issue with binding GridViewSelectColumn from ViewModel

3 Answers 105 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jebouy
Top achievements
Rank 1
Jebouy asked on 11 Apr 2011, 05:38 PM
 

Hi,

   I am having an MVVM application  I am not able to view GridViewSelectColumn as selected  even after setting the "selected" property from viewmodel. Below is the code

From UI it is working fine. If you can give your email id then i can send the sample code.  

 

 

 

<telerikGridView:RadGridView 
            x:Name="grdEntity"
            ItemsSource="{Binding Entities}"     
            RowIndicatorVisibility="Collapsed"    
            AutoGenerateColumns="False"                     
            IsReadOnly="True"
            HorizontalAlignment="Left" 
            ShowGroupPanel="False" 
            GridLinesVisibility="Horizontal"                                                            
            Margin="0"
            Grid.Row="0" 
            Width="200" 
            SelectionMode="Multiple"                
            cmd:RadGridExtensions.SelectAll="{Binding SelectAll, Mode=TwoWay}"
            >
            <telerikGridView:RadGridView.Columns>
                  
                <telerikGridView:GridViewSelectColumn Width="25"/>
                             
                <telerikGridView:GridViewDataColumn 
                            Header="Name"
                            UniqueName="Name" 
                            IsGroupable="False" 
                            TextWrapping="Wrap"/>
            </telerikGridView:RadGridView.Columns>
        </telerikGridView:RadGridView>
  
  
  
/***********************
Business Entity
*********************/
public class Entity : INotifyPropertyChanged, IRadGridMultiSelect
    {
        private bool m_selected;
        private string m_name;
  
        public string Name
        {
            get
            {
                return m_name;
            }
            set
            {
                m_name = value;
                OnPropertyChanged("Name");
            }
        }
  
        public bool Selected
        {
            get { return m_selected; }
            set { m_selected = value;
            OnPropertyChanged("Selected");
            }
        }
        #region INotifyPropertyChanged Members
  
        public event PropertyChangedEventHandler PropertyChanged;
  
        #endregion
  
        #region IRadGridMultiSelect Members
  
        public void Select(bool val)
        {
            this.Selected = val;
        }
  
        #endregion
  
        void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
  
/****************************
View Model
**********************************/
 public class MainPageViewModel : INotifyPropertyChanged
    {
  
        #region Private Members
  
        private ICommand _buttonClickedCommand;
         
        private ObservableCollection<Entity> m_entities = new ObservableCollection<Entity>();
        private bool m_SelectAll;
        private string m_Count;
  
        #endregion
  
        #region Constructor
  
        public MainPageViewModel()
        {
            CreateEntities();
        }
  
        #endregion
  
        #region Public Members
  
        private void CreateEntities()
        {
            Entities.Add(new Entity() { Selected = true, Name = "Joe" });
            Entities.Add(new Entity() { Selected = true, Name = "Rick" });
            Entities.Add(new Entity() { Selected = false, Name = "Nate" });
            Entities.Add(new Entity() { Selected = false, Name = "Jim" });
        }
        public ICommand ButtonClickedCommand
        {
            get
            {
                if (_buttonClickedCommand == null)
                    _buttonClickedCommand = new RelayCommand((p) => OnCountClicked(p), null);
                return _buttonClickedCommand;
            }
        }
  
         
        public string Count
        {
            get { return m_Count; }
            set
            {
                m_Count = value;
                this.OnPropertyChanged("Count");
            }
        }
  
  
        public bool SelectAll
        {
            get { return m_SelectAll; }
            set
            {
                m_SelectAll = value;
                this.OnPropertyChanged("SelectAll");
            }
        }
        public ObservableCollection<Entity> Entities
        {
            get
            {
                return m_entities;
            }
            set
            {
                m_entities = value;
                OnPropertyChanged("Entities");
            }
        }
  
        #endregion
  
        #region Event Handlers
  
        public void OnCountClicked(object args)
        {
            Count = Entities.Where(x => x.Selected == true).Count().ToString();
        }
  
         
  
        #endregion
  
        #region INotifyPropertyChanged Members
  
        public event PropertyChangedEventHandler PropertyChanged;
  
        void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
  
        #endregion
    }
  
/*******************************
RadGrid Extension
********************************/
  public interface IRadGridMultiSelect
    {
        void Select(bool val);
    }
   public static class RadGridExtensions
    {                      
  
        #region SelectAll
  
        public static bool GetSelectAll(DependencyObject obj)
        {
            return (bool)obj.GetValue(SelectAllProperty);
        }
  
        public static void SetSelectAll(DependencyObject obj, bool value)
        {
            obj.SetValue(SelectAllProperty, value);
        }
  
        // Using a DependencyProperty as the backing store for SelectAll.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectAllProperty =
            DependencyProperty.RegisterAttached("SelectAll", typeof(bool), typeof(RadGridExtensions), new PropertyMetadata(true, OnSelectAllChanged));
  
        //public static int count = 0;
        private static void OnSelectAllChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var radGrid = (RadGridView)d;
  
            if (radGrid.Tag == null )
            {
                radGrid.SelectionChanged += (s, ev) =>
                {
                    ReSetSelectAll(radGrid, ev);
                };
                radGrid.Tag = "1";
            }          
             
        }
  
        private static void ReSetSelectAll(RadGridView radGrid, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            if (radGrid.ItemsSource != null)
            {
                foreach (var item in e.AddedItems)
                {
                    var i = item as IRadGridMultiSelect;
                    if (i != null)
                        i.Select(true);
                }
                foreach (var item in e.RemovedItems)
                {
                    var i = item as IRadGridMultiSelect;
                    if (i != null)
                        i.Select(false);
                }
  
                //if (((IList)radGrid.ItemsSource).Count == radGrid.SelectedItems.Count)
                //{
                //    SetSelectAll(radGrid, false);
                //    SetSelectAll(radGrid, true);
                //}
                //else if (radGrid.SelectedItems.Count == 0)
                //{
                //    SetSelectAll(radGrid, false);
                //}
            }
                         
        }
  
        #endregion     
  
    }

3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 11 Apr 2011, 05:48 PM
Hi Jebouy,

May you provide a bit more details about the exact scenario you want to accomplish ? Generally, the IsChecked property of each of the CheckBox-es in the column is bound to the IsSelected property of the corresponding row. So, you may use the SelectedItem or SelectedItems collection of the grid.
 

All the best,
Maya
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
Jebouy
Top achievements
Rank 1
answered on 11 Apr 2011, 06:04 PM
Hi,
    
 Here my scenario is:  there are certain records already got saved. In the update screen i have to show the previous saved records as selected while initial load.

  in the  CreateEntities() method i have two items have selected properties as true. which is not reflecting in the grid UI.
private void CreateEntities() 
        
            Entities.Add(new Entity() { Selected = true, Name = "Joe" }); 
            Entities.Add(new Entity() { Selected = true, Name = "Rick" }); 
            Entities.Add(new Entity() { Selected = false, Name = "Nate" }); 
            Entities.Add(new Entity() { Selected = false, Name = "Jim" }); 
        }

I am not able to attach any zip file here. other wise i could have to send the sample code.
0
Vlad
Telerik team
answered on 12 Apr 2011, 06:58 AM
Hi,

 You can attach zips only in support tickets. 

Greetings,
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
Tags
GridView
Asked by
Jebouy
Top achievements
Rank 1
Answers by
Maya
Telerik team
Jebouy
Top achievements
Rank 1
Vlad
Telerik team
Share this question
or