I am using an MVVM pattern for binding my UI. I am using ICommand's to attach the handlers. I created a "Select All" checkbox in the Header of a GridViewDataColumn, but every time I pass the commandParameter of the SelectAll checkbox itself, it comes up null.
However, it worked perfectly if I take the EXACT SAME checkbox and move it EITHER completely outside of the RadGridView, or even into a CellTemplate. It appears the binding is occurring before the Header has been created. Or am I just creating a "Select All" completely wrong?
XAML:
ViewModel command handler:
However, it worked perfectly if I take the EXACT SAME checkbox and move it EITHER completely outside of the RadGridView, or even into a CellTemplate. It appears the binding is occurring before the Header has been created. Or am I just creating a "Select All" completely wrong?
XAML:
<
UserControl
>
<
UserControl.Resources
>
<
c:InfoBaseViewModel
x:Key
=
"InfoBaseViewModel2"
/>
</
UserControl.Resources
>
<
DockPanel
Name
=
"InfoDockPanel"
DataContext
=
"{StaticResource InfoBaseViewModel2}"
>
<
telerik:RadGridView
x:Name
=
"grdInfo"
AutoGenerateColumns
=
"False"
IsReadOnly
=
"True"
ItemsSource
=
"{Binding Path=InfoList}"
ShowGroupPanel
=
"False"
>
<
telerik:GridViewDataColumn
>
<
telerik:GridViewDataColumn.Header
>
<
CheckBox
Name
=
"chkAll"
CommandParameter
=
"{Binding ElementName=chkAll}"
Command
=
"{Binding AllCheckedCommand,Source={StaticResource InfoBaseViewModel2}}"
></
CheckBox
>
</
telerik:GridViewDataColumn.Header
>
<
telerik:GridViewDataColumn.CellTemplate
>
<
DataTemplate
>
<
CheckBox
IsChecked
=
"{Binding Path=IsItemSelected, Mode=TwoWay}"
></
CheckBox
>
</
DataTemplate
>
</
telerik:GridViewDataColumn.CellTemplate
>
<
telerik:GridViewDataColumn
DataMemberBinding
=
"{Binding FullName}"
Header
=
"{Binding Path=ApplicationStrings.FullNameLabel, Source={StaticResource ResourceWrapper}}"
></
telerik:GridViewDataColumn
>
</
telerik:GridViewDataColumn
>
</
telerik:RadGridView
>
</
DockPanel
>
</
UserControl
>
ViewModel command handler:
public
ICommand AllCheckedCommand
{
get
{
return
_allItemsChecked ?? (_allItemsChecked =
new
RelayCommand(AllCheckedHandler,
null
)); }
}
public
void
AllCheckedHandler(
object
sender)
{
// Always gets to here...
var chkAll = sender
as
System.Windows.Controls.CheckBox;
if
(chkAll ==
null
)
{
// ...but always fails
Logger.Error(
"Error binding the 'Select All' checkbox."
);
}
else
{
foreach
(var item
in
InfoList)
{
item.IsItemSelected = (
bool
)chkAll.IsChecked;
}
base
.OnPropertyChanged(
"InfoList"
);
}
}