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

Bound GridViewCheckBoxColumn, what event to use?

3 Answers 353 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rob Ainscough
Top achievements
Rank 1
Rob Ainscough asked on 11 Oct 2012, 09:03 PM
I'm not having much success in what I thought would be a simple process (at least is was in Windows Forms apps).

I have a GridView bound to an observableCollection ... columns are being displayed correctly with bound data.  I simply want to do some additional processing when my GridViewCheckBoxColumn is checked (or unchecked).

Any hints on how to accomplish this?  I've surfed and read the documentation and there is NOTHING about how to respond to a GridViewCheckBoxColumn event -- I'm guessing this must be a pretty common task??  But so far, I haven't been able to find any event that will consistently work with a GridViewCheckBoxColumn check/uncheck.

Thanks, Rob.

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 12 Oct 2012, 05:39 AM
Hello,

 You should not use events in both WPF in Silverlight. Unlike WinForms here MVVM is the king! You can add your additional logic in your property setter, and/or in your custom view-model and/or handling PropertyChanged of your INotifyPropertyChanged data items, etc.

Kind regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Rob Ainscough
Top achievements
Rank 1
answered on 12 Oct 2012, 03:22 PM
MVVM pattern is code bloat in it's best case and in it's worse case it can not handle simple and common situations.  It's based on a false assumption that the UI and business logic are separate entities ... in simple cases this is true, in more complex cases it's false ... add Silverlight 5's "partial" implementation (no secret Microsoft just wanted to get SL5 out the door and off their hands). 

MVVM Issues:
1.  There just is NO ROI (return on investment)
2.  It's slow
3.  More code is never a good thing
4.  No two programmers ever agree on exactly how to implement MVVM (it's very inconsistent)
5.  It's only effective on simple to moderate applications

With that said I use a mix of MVVM and code behind as this is the most efficient.  But your suggestion of PropertyChanged and using INotifyPropertyChanged does not work in this situation because the GridView is bound to an ObservableCollection of Objects and no object's property events are Notifiable without adding yet another layer of abstraction.  The more layers you add, the slower the application gets and the more difficult it is to maintain as you increase risk of breaking A LOT more as you much up the chain of abstraction -- this IS NOT a good thing.

I was able to find a simple and elegant solution that is not MVVM but works well, and is faster than MVVM implementation and much easier to maintain.

XAML:
<telerik:GridViewDataColumn x:Name="ServiceSelectedColumn" Header="" DataMemberBinding="{Binding Selected, Mode=TwoWay}" IsReadOnly="True">
    <telerik:GridViewDataColumn.CellTemplate>
        <DataTemplate>
            <CheckBox x:Name="ServiceSelectedCheckBox" IsChecked="{Binding Selected, Mode=TwoWay}" Tag="{Binding}" Click="ServiceSelectedCheckBox_Click"/>
        </DataTemplate>
    </telerik:GridViewDataColumn.CellTemplate>

Code Behind:
Private Sub ServiceSelectedCheckBox_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
 
    If Not sender Is Nothing Then
 
        ' The current service that was selected
        Dim aService As DC.SL.Services.WebServiceSite.Services = TryCast(CType(sender, CheckBox).Tag, DC.SL.Services.WebServiceSite.Services)
 
        If aService.Selected Then
            Me.DataContext.ServicesTotal = Me.DataContext.ServicesTotal + aService.ServiceAmount
        Else
            Me.DataContext.ServicesTotal = Me.DataContext.ServicesTotal - aService.ServiceAmount
        End If
 
    End If
 
End Sub

Me.DataContext.ServicesTotal is part of the view model with OnPropertyChanged support.  Hybrid use of MVVM and Code Behind for the solution.

Rob

P.S.  Vlad, MVVM is not king and never will be king for the reasons I stated above, do any complex large scale application using MVVM exclusively and you WILL run into performance issues and most importantly maintenance issues because the pattern tries (and fails) to solve every UI situation.  MVVM was over sold, over hyped, and promotes code bloat.
0
Hai
Top achievements
Rank 1
answered on 13 Apr 2020, 04:27 PM

I know this is 8 years old post, but for me WPF is brand new, my very first project and I ran into this issue. I spent hours on google with no luck, lot of frustration, until I found this post. Thanks Rob Ainscough. Coming from WinForm's world, I find this is the simpliest way I can related to. So here is C# version in case it helps anyone else

private void GridViewCheckBoxColumn_Clicked(object sender, RoutedEventArgs e)
{
    if(sender!= null)
    {
        var c = (CheckBox)sender;
        if(c.IsChecked==true)
        {
            var vm = ((PropertyViewModelPLA)c.Tag);
            //DO Thing with vm
        }         
    }
}
<telerik:GridViewColumn>
    <telerik:GridViewColumn.CellTemplate>
        <DataTemplate>
            <CheckBox x:Name="_checkbox"
            IsChecked="{Binding Selected, Mode=TwoWay}"
            Tag="{Binding}"
            Click="GridViewCheckBoxColumn_Clicked"/>
        </DataTemplate>
    </telerik:GridViewColumn.CellTemplate>
</telerik:GridViewColumn>

        }

Tags
GridView
Asked by
Rob Ainscough
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Rob Ainscough
Top achievements
Rank 1
Hai
Top achievements
Rank 1
Share this question
or