Reputation: 479
I have a wpf application with datagrid and it has datapicker which has date value. If the user changes the date then some logic is run and a popup displayed as part of selectionChangedEvent hanlder code. If they choose 'No' on the popup then I Want to reset the date...Unable to figure this part
MyXaml
<DataGridTemplateColumn Header="Start Date">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<DatePicker Name="StartDate" SelectedDate="{Binding StartDate}" BorderThickness="0" SelectedDateChanged="StartDate_SelectedDateChanged">
<DatePicker.Resources>
<Style TargetType="DatePickerTextBox">
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="IsEnabled" Value="False"/>
</Style>
</DatePicker.Resources>
</DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Code
Private Sub ValidateDateChanged(sender As Object, e As SelectionChangedEventArgs)
If MessageBoxResult.No = MessageBox.Show("Yes to Continue with the change",
vbApplicationModal + MsgBoxStyle.Information, MessageBoxButton.YesNo) Then
how to reset the date value to the original value and stop event handler to not invoke again for this ovriden date change
was trying this e.source.SelectedDate = CType(e.RemovedItems(0), Date)
End If
Sub
UPDATE: Problem is two additional events for the same datepicker are fired causing the popup to be displayed three times. This happens at the time of the load also
Upvotes: 0
Views: 168
Reputation: 169200
You could use a Boolean
variable to temporarily suspend the event handler from actually doing something, e.g.:
Private _handleEvent As Boolean = True
Private Sub ValidateDateChanged(sender As Object, e As SelectionChangedEventArgs)
If _handleEvent And MessageBoxResult.No = MessageBox.Show("Yes to Continue with the change",
vbApplicationModal + MsgBoxStyle.Information, MessageBoxButton.YesNo) Then
Dim dp As DatePicker = CType(sender, DatePicker)
_handleEvent = False
dp.SelectedDate = CType(e.RemovedItems(0), Date?)
_handleEvent = True
End If
End Sub
Upvotes: 1