I have a grid with most of the columns declared as "GridViewCheckBoxColumn"'s (new in the latest internal build). What I have noticied is when i am in a grid and I am editing a text column and tab off, the GridViewCellEditEnded event kicks off. However, when I click in a checkbox column (toggleing the checkbox to true or false) this event doesn't fire. Is there an event for these checkbox columns? Do i need to addhandlers for some event?
Basically, all i want is an event to fire when i check or uncheck a checkbox (a cell edit ended event).
| private void DgContactsCellEditEnded(object sender, GridViewCellEditEndedEventArgs e) |
| { |
| ...... |
| } |
Please advise, Thanks!
14 Answers, 1 is accepted
I have tested this scenario and found that the CellEditEnded is fired the same way as in all other columns.
To see this in action , please check the project attached. Once you leave a cell after editing , you will get the event fired.
Kind regards,
Pavel Pavlov
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.
Thank you for taking the time to create a sample application. I also opened a support ticket with Nedyalko Nikolov and he too is saying that this is the way the event should work.
What happens is when i check or uncheck the checkbox and then click off the row, the event fires.
I am looking for an event to fire as soon as the checkbox is checked or unchecked...for example, a radcheckbox control, there is a checked event that fires immediatly when the checkbox is clicked...is there such an event?
Thanks,
To achieve the required behavior you may need a slightly different approach . The CheckBox column is not suitable for this scenario.
You will need to place a CheckBox in the cell using the Column.CellTemplate property. For more information you can take a look at this online example.
Within this datatemplate you may subscribe to the Clicked event of the checkbox.
This way the event may be handled at the moment the user clicks, rather than waiting the cell to leave edit mode.
Sincerely yours,
Nedyalko Nikolov
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.
This is what my code looks like:
| private void AddColumns(ObservableCollection<Contact> contacts) |
| { |
| if (contacts.Count > 0) |
| { |
| for (var ct = 0; ct < contacts[0].Lists.Count; ct++) |
| { |
| var cl = new GridViewCheckBoxColumn(); (); |
| cl.Header = contacts[0].Lists[ct].ListName; |
| cl.AutoSelectOnEdit = true; |
| cl.DataMemberBinding = new System.Windows.Data.Binding("List" + ct); |
| dgContacts.Columns.Add(cl); |
| } |
| } |
| } |
How do i implement a regular checkbox in code?
Thanks!
As Nedyalko said you will need to define the checkbox in a DataTemplate so you can subscribe to the click event.
The fact that you create the columns in code does not conflict with this approach. You will need to define the cell template in XAML , as a static resource with a key e.g "MyCellTemplate"
Then in your code behind you may do something like :
MyNewlyCreatedColumnInCode.CellTemplate = this.Resources["MyCellTemplate"]
Regards,
Pavel Pavlov
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.
| <telerik:RadGridView HorizontalAlignment="Left" Margin="5,10,0,0" telerik:StyleManager.Theme="Vista" |
| CellEditEnded="DgContactsCellEditEnded" RowLoaded="DgContactsRowLoaded" |
| Name="dgContacts" VerticalAlignment="Top" AutoGenerateColumns="false"> |
| <telerik:RadGridView.Columns> |
| <telerik:GridViewDataColumn DataMemberBinding="{Binding ContactId, Mode=TwoWay}" Header="ContactId" IsReadOnly="True" /> |
| <telerik:GridViewDataColumn DataMemberBinding="{Binding FullName, Mode=TwoWay}" Header="FullName" IsReadOnly="True" /> |
| </telerik:RadGridView.Columns> |
| </telerik:RadGridView> |
Now depending on what my stored procedure brings back, I could have from 0 to 10 more columns, So I build the rest dynamically in the addcolumns functions found in this thread.
Now, I am not sure on how to define the cell template in XAML as a static resource with a key e.g "MyCellTemplate" if you could provide the XAML.
Thanks,
Hello,
So my XAML is the following:
| <UserControl.Resources> |
| <DataTemplate x:Key="CheckboxTemplate"> |
| <CheckBox IsEnabled="True" Checked="CheckBox_Checked" Click="CheckBox_Click" IsThreeState="False" Resources="CheckboxTemplate" |
| VerticalAlignment="Center" Loaded="Editor_Loaded" /> |
| </DataTemplate> |
| </UserControl.Resources> |
And my code is the following:
| private void AddColumns(ObservableCollection<Contact> contacts) |
| { |
| if (contacts.Count > 0) |
| { |
| for (var ct = 0; ct < contacts[0].Lists.Count; ct++) |
| { |
| var cl = new GridViewDataColumn(); |
| cl.Header = contacts[0].Lists[ct].ListName; |
| cl.DataMemberBinding = new System.Windows.Data.Binding("List" + ct); |
| cl.CellTemplate = (DataTemplate)(this.Resources["CheckboxTemplate"]); |
| dgContacts.Columns.Add(cl); |
| } |
| } |
| } |
I am receiving the following exception:
| - ExceptionObject {MS.Internal.WrappedException: Failed to create a 'System.Windows.ResourceDictionary' from the text 'CheckboxTemplate'. [Line: 13 Position: 121] ---> System.Windows.Markup.XamlParseException: Failed to create a 'System.Windows.ResourceDictionary' from the text 'CheckboxTemplate'. [Line: 13 Position: 121] |
| at MS.Internal.XcpImports.MethodEx(IntPtr ptr, String name, CValue[] cvData) |
| at MS.Internal.XcpImports.MethodEx(DependencyObject obj, String name) |
| at MS.Internal.XcpImports.DataTemplate_LoadContent(DataTemplate template) |
| at System.Windows.DataTemplate.LoadContent() |
| at Telerik.Windows.Controls.GridViewColumn.CreateCellElement(GridViewCell cell, Object dataItem) |
| at Telerik.Windows.Controls.GridViewBoundColumnBase.CreateCellElement(GridViewCell cell, Object dataItem) |
| at Telerik.Windows.Controls.GridView.GridViewCell.CreateCellElement(GridViewColumn column, Object dataItem) |
| at Telerik.Windows.Controls.GridView.GridViewCell.BuildVisualTree() |
| at Telerik.Windows.Controls.GridView.GridViewCell.SetCellElement() |
| at Telerik.Windows.Controls.GridView.GridViewRow.PrepareCell(GridViewCellBase cellBase, GridViewColumn column) |
| at Telerik.Windows.Controls.GridView.DataCellsPresenter.PrepareContainerForItemOverride(DependencyObject element, Object item) |
| at System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item) |
| at System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container) |
| at Telerik.Windows.Controls.GridView.GridViewCellsPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled) |
| at Telerik.Windows.Controls.GridView.GridViewCellsPanel.InsertNewContainer(Int32 childIndex, UIElement container) |
| at Telerik.Windows.Controls.GridView.GridViewCellsPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized) |
| at Telerik.Windows.Controls.GridView.GridViewCellsPanel.GenerateChild(IItemContainerGenerator generator, Size constraint, GridViewColumn column, Int32& childIndex, Size& childSize) |
| at Telerik.Windows.Controls.GridView.GridViewCellsPanel.GenerateChild(IItemContainerGenerator generator, Size constraint, GridViewColumn column, IDisposable& generatorState, Int32& childIndex, Size& childSize) |
| at Telerik.Windows.Controls.GridView.GridViewCellsPanel.DetermineRealizedColumnsBlockList(Size constraint) |
| at Telerik.Windows.Controls.GridView.GridViewCellsPanel.MeasureOverride(Size constraint) |
| at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight) |
| --- End of inner exception stack trace ---} System.Exception {MS.Internal.WrappedException} |
Message "Failed to create a 'System.Windows.ResourceDictionary' from the text 'CheckboxTemplate'. [Line: 13 Position: 121]" string
Please advise.
Please help.
Here is my custom column,
Imports
System
Imports
System.Net
Imports
System.Windows
Imports
System.Windows.Controls
Imports
System.Windows.Documents
Imports
System.Windows.Ink
Imports
System.Windows.Input
Imports
System.Windows.Media
Imports
System.Windows.Media.Animation
Imports
System.Windows.Shapes
Imports
Telerik.Windows.Controls
Imports
Telerik.Windows.Controls.GridView
Imports
System.Windows.Controls.Primitives
 
Public
Class CustomColumn
Inherits GridViewDataColumn
 
'EventManager(GetType(TheClassThatWillHandleRadMenuItemsClik), RadMenuItem.ClickEvent, New RoutedEventHandler(OnMenuClick), True)
 
Public Overrides Function CreateCellElement(ByVal cell As GridViewCell, ByVal dataItem As Object) As FrameworkElement
Dim evt As New EventPage
Dim cb = TryCast(cell.Content, CheckBox)
If cb Is Nothing Then
cb =
New CheckBox()
cb.Height = 20
cb.SetBinding(
CheckBox.IsCheckedProperty, Me.DataMemberBinding)
cell.Content = cb
cell.SetBinding(
GridViewCell.ValueProperty, Me.DataMemberBinding)
End If
If cell.Value = "True" Then
cell.Background =
New SolidColorBrush(Colors.Green)
End If
 
Return cb
End Function
End
Class
Tim,
I didn’t end up using the custom class, I had to use the windows checkbox column. I didn’t end up using the telerik grid, it gave me problems down the road.
I did use this event to capture the windows checkbox changing:
private void MyDataGridCurrentCellChanged(object sender, EventArgs e)
{
if (myDataGrid.CurrentColumn is DataGridCheckBoxColumn)
{
myDataGrid.BeginEdit();
}
}
Thank you very much. You saved the day.
Tim
I think I used this event:
private void MyDataGridContactsPreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
var checkBox = e.EditingElement as CheckBox;
if (checkBox != null)
{
checkBox.IsChecked = !checkBox.IsChecked;
}
}