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
}