【问题标题】:how to reset the datapicker date value to original value如何将 datepicker 日期值重置为原始值
【发布时间】:2018-04-23 21:16:01
【问题描述】:

我有一个带有数据网格的 wpf 应用程序,它有具有日期值的数据选择器。如果用户更改日期,则运行一些逻辑并显示一个弹出窗口作为 selectionChangedEvent 处理程序代码的一部分。如果他们在弹出窗口中选择“否”,那么我想重置日期...无法计算这部分

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>

代码

     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

更新: 问题是同一日期选择器的两个附加事件被触发,导致弹出窗口显示三次。这也发生在加载时

【问题讨论】:

  • 可以从SelectedDateChanged事件的事件参数中获取新旧值。
  • 问题是事件会在以编程方式更改值时再次触发,导致弹出窗口在重置时显示
  • 我了解您的问题。在这种情况下,我可能会做的是保留一个标志来跟踪值是如何变化的。如果您正在重置,您可以将此标志标记为真,否则为假。如果我有更好的方法,我稍后会建议你。
  • 抱歉昨天没能查到。如果您使用依赖属性作为绑定到日期时间选择器的支持字段,我还有一个建议给您。

标签: wpf vb.net datepicker


【解决方案1】:

您可以使用Boolean 变量来临时暂停事件处理程序的实际操作,例如:

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

【讨论】:

  • 我已经尝试过这种方法,但它不起作用。我面临的问题是有两个额外的选择更改事件正在为同一个日期选择器触发,导致弹出窗口再显示两次。总共三个弹出窗口
  • 尽管我不喜欢它..我仍然不得不使用 count 变量来忽略额外的事件触发..不确定为什么它会触发两个额外的事件。问题类似于social.msdn.microsoft.com/Forums/vstudio/en-US/…
猜你喜欢
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-16
  • 2015-11-23
  • 2012-07-15
相关资源
最近更新 更多