How to Cancel edit on GridCheckBoxColumn
I have an sfdatagrid and one of the columns is a GridCheckBoxColumn bound to an underlying Boolean value called "Derive". I want to add validation such that only one element in the item source can have Derive == true.
I've managed to achieve this by trapping the CurrentCellValueChanged event and setting the value back to false if the count is too high:-
private void ProfilesGrid_CurrentCellValueChanged(object sender, CurrentCellValueChangedEventArgs e)
{
if (e.Column.MappingName == "Derive")
{
if ((AlgorithmConfiguration as GA3LiteAlgorithmConfiguration).DecisionVariablesProfiles.Count(p => p.Derive) > 1)
{
MessageBox.Show("Cannot have more than one decision profile in Genetic Algorithm Lite");
(e.Record as GA3DecisionVariableConfiguration).Derive = false;
}
}
}
However, I fell this is a bit hacky as it means the value is changed and then changed back, meaning a whole bunch of notifications fire unnecessarily in the background. I would rather trap this in something like the CurrentCellValidating event and setting Cancel to true to avoid the underlying value changing at all. Something like:-
private void ProfilesGrid_CurrentCellValidating(object sender, CurrentCellValidatingEventArgs e)
{
if (e.Column.MappingName == "Derive")
{
if ((AlgorithmConfiguration as GA3LiteAlgorithmConfiguration).DecisionVariablesProfiles.Count(p => p.Derive) > 0)
{
e.IsValid = false;
e.ErrorMessage = "Only one decision profile allowed in Genetic Algorithm Lite";
}
}
}
However, the CurrentCellValidating event doesn't fire. I also tried the CurrentCellBeginEdit event to refuse entering edit mode but, similarly, it doesn't fire. I'm guessing the check box isn't considered the current cell.
What is the recommended way of refusing validation on a data bound DataGridCheckBoxColumn?
Hi Declan Hillier,
We are currently analyzing the scenario you reported. We need time to validate it, and we will provide an update on or before January 08, 2024.
Regards,
Santhosh.G
Hi Declan Hillier,
Thank you for your patience.
Your requirement can be achieved by overriding the checkbox column
renderer and using the PreviewMouseDown event. By handling this event, you can
control the checkbox value change based on your condition before it changes.
Code Snippet:
|
public MainWindow() { InitializeComponent();
dataGrid.CellRenderers.Remove("CheckBox"); dataGrid.CellRenderers.Add("CheckBox",new GridCheckBoxRendererExt(this.dataGrid)); } public class GridCheckBoxRendererExt : GridCellCheckBoxRenderer { SfDataGrid SfDataGrid1; public GridCheckBoxRendererExt(SfDataGrid dataGrid) { SfDataGrid1 = dataGrid; } protected override void OnEditElementLoaded(object sender, RoutedEventArgs e) {
(sender as CheckBox).PreviewMouseDown += GridCheckBox_PreviewMouseDown;
}
private void GridCheckBox_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
var itemsource = SfDataGrid1.ItemsSource as ObservableCollection<OrderInfo>; if ((e.Source as CheckBox).IsChecked == false && itemsource != null && itemsource.Count(p => p.Derive) >= 1) { e.Handled = true; } }
|
For your reference, we have attached a sample and a video. Please
review them.
Regards,
Santhosh.G
Attachment: Demo_3239c5a8.zip
Works like a charm. Thank you.
Hi Declan Hillier,
We are glad to know
that the reported problem has been resolved at your end. Please let us know if
you have any further queries on this. We are happy to help.
Regards,
Santhosh.G
- 4 Replies
- 2 Participants
- Marked answer
-
DH Declan Hillier
- Jan 3, 2025 02:43 PM UTC
- Jan 10, 2025 03:56 AM UTC