【问题标题】:DateTimePicker OnValueChanged event is raised twiceDateTimePicker OnValueChanged 事件引发两次
【发布时间】:2025-11-24 07:05:01
【问题描述】:

我正在使用来自 Extended WPF Toolkit Community Edition 库版本 2.5 的 DateTimePicker WPF 控件。

我的问题是,当我选择一个日期时,OnValueChanged 事件会引发两次而不是一次。

这是我正在使用的代码:

XAML:

<StackPanel>
    <xctk:DateTimePicker AutoCloseCalendar="True"  Name="picker"  Width="400" Height="40" ValueChanged="UpDownBase_OnValueChanged"/>
    <ListBox Height="300" Name="listbox"></ListBox>
</StackPanel>

后面的C#代码:

private void UpDownBase_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    var value = picker.Value;

    if (value == null)
        listbox.Items.Add("[NULL]");
    else
        listbox.Items.Add(value.Value.ToString(CultureInfo.InvariantCulture));
}

现在,每当我选择一个新日期时,列表框都会填充两个新项目。我还调试了程序,确认事件处理程序实际上被调用了两次。

我该如何解决这个问题?

更新:

我尝试了 2.4 版,但问题似乎消失了。现在在我看来,这可能是 2.5 版中的一个错误。

【问题讨论】:

  • 你有没有在一个空项目中用上面的代码试过?如果是这样,如果没有其他人回答,我会尽快测试。
  • 是的。这段代码实际上来自我为解决问题而创建的一个空项目。

标签: c# wpf wpftoolkit


【解决方案1】:

这似乎是因为 2.5 中的事件是从以下位置触发的:

at Xceed.Wpf.Toolkit.DateTimePicker.OnValueChanged(Nullable`1 oldValue, Nullable`1 newValue) in C:\Users\Mark Vinten\Downloads\wpftoolkit-114314\Main\Source\ExtendedWPFToolkitSolution\Src\Xceed.Wpf.Toolkit\DateTimePicker\Implementation\DateTimePicker.cs:line 264

然后从基类开始:

at Xceed.Wpf.Toolkit.TimePicker.OnValueChanged(Nullable`1 oldValue, Nullable`1 newValue) in C:\Users\Mark Vinten\Downloads\wpftoolkit-114314\Main\Source\ExtendedWPFToolkitSolution\Src\Xceed.Wpf.Toolkit\TimePicker\Implementation\TimePicker.cs:line 264

现在基类,似乎也经历了CLR绑定过程,提示这是绑定值。我仍在研究为什么会这样,但一种解决方法是像这样使用 Binding:

MainWindow.cs

    public DateTime? DateTimeValue
    {
        get { return (DateTime?)GetValue(DateTimeValueProperty); }
        set { SetValue(DateTimeValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for DateTimeValue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DateTimeValueProperty =
        DependencyProperty.Register("DateTimeValue", typeof(DateTime?), typeof(MainWindow), new PropertyMetadata(null, new PropertyChangedCallback(DateTimeValueProperty_Changed)));

    private static void DateTimeValueProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MainWindow mw = d as MainWindow;
        System.Diagnostics.Debug.WriteLine("d is " + d == null ? "null" : d.GetType().FullName);
        if (mw != null && e.Property == DateTimeValueProperty)
        {
            var value = e.NewValue as DateTime?;
            var listbox = FindChild<ListBox>(mw, "listbox");

            if (value == null)
                listbox.Items.Add("[NULL]");
            else
                listbox.Items.Add(value.Value.ToString(System.Globalization.CultureInfo.InvariantCulture));
        }
    }

    /// <summary>
    /// Finds a Child of a given item in the visual tree. 
    /// </summary>
    /// <param name="parent">A direct parent of the queried item.</param>
    /// <typeparam name="T">The type of the queried item.</typeparam>
    /// <param name="childName">x:Name or Name of child. </param>
    /// <returns>The first parent item that matches the submitted type parameter. 
    /// If not matching item can be found, 
    /// a null parent is being returned.</returns>
    public static T FindChild<T>(DependencyObject parent, string childName)
       where T : DependencyObject
    {
        // Confirm parent and childName are valid. 
        if (parent == null) return null;

        T foundChild = null;

        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            // If the child is not of the request child type child
            T childType = child as T;
            if (childType == null)
            {
                // recursively drill down the tree
                foundChild = FindChild<T>(child, childName);

                // If the child is found, break so we do not overwrite the found child. 
                if (foundChild != null) break;
            }
            else if (!string.IsNullOrEmpty(childName))
            {
                var frameworkElement = child as FrameworkElement;
                // If the child's name is set for search
                if (frameworkElement != null && frameworkElement.Name == childName)
                {
                    // if the child's name is of the request name
                    foundChild = (T)child;
                    break;
                }
            }
            else
            {
                // child element found.
                foundChild = (T)child;
                break;
            }
        }

        return foundChild;
    }

MainWindow.xaml

    <StackPanel DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}">
        <xctk:DateTimePicker AutoCloseCalendar="True"  Name="picker"  Width="400" Height="40" Value="{Binding DateTimeValue}" />
        <ListBox Height="300" Name="listbox"></ListBox>
    </StackPanel>

这使用绑定系统自动检查值是否已更改,如果已更改则仅引发事件。

注意:FindChild 是我在 How can I find WPF controls by name or type? 帖子中找到的一个函数

更新最终摘要

原因似乎是因为在 DateTimePicker 中嵌入了一个 TimePicker 以提供该功能。不幸的是,DateTimePicker 和 TimePicker 都派生自同一个基数,因此在 UpDownBase 中引发相同的路由事件,其中 T 是 DateTime?。

如果您检查事件参数,则 e.RoutedEVent 始终为 UpDownBase.OnValueChanged,因为这是引发事件的类。 e.Source 或 e.OriginalSource 始终是 DateTimePicker 本身,这意味着您没有有用的方法来过滤掉一个或另一个事件。

DateTimeUpDown.RaiseValueChangedEvent() 中有代码来检查 TemplatedParent 是否是 TimePicker 以防止重新引发,但无论是从 DateTimePicker 还是 TimePicker 引发事件,TemplatedParent 似乎总是是 DateTimePicker 所以失败因此你获得两次事件。

我在 WPFToolkit 项目站点上的发现中提出了一个错误: https://wpftoolkit.codeplex.com/workitem/22014

【讨论】:

  • 感谢您的回答和努力。我尝试了解决方法并且它有效。但是,如果您在同一个窗口上有多个 DateTimePicker 对象,我不确定这是否可以工作。有办法吗?您认为仅使用 2.4 版本有什么问题吗?
  • 可以,但是您需要不同的选定值支持,这是我最近更喜欢的东西,因为它可以让您更清楚地了解正在发生的事情,这些将通过不同的属性和发件人显示DateTimeValueProperty_Changed 即使您重复使用相同的静态方法。新版本总是有更新的功能和错误修复(以及固有的错误),因此您只需选择适合您的版本。
  • When the picker loses focus (when I select another control, e.g. the listbox) the changed event is fired, I think I have to manually check for the value in the event handler.我用多个选择器测试了你的解决方法,它可以工作。在修复错误之前,我将使用 V2.4 或使用您的解决方法。
【解决方案2】:

我通过检查事件的原始来源解决了这个问题/错误。

 if(e.OriginalSource is Xceed.Wpf.Toolkit.DateTimePicker)
    {
       if(((Xceed.Wpf.Toolkit.DateTimePicker)e.OriginalSource).IsFocused == true)
       {
          ResetDataTable();
       }
    }

由于我没有在此控件中显示时间选择器,因此我还要确保它是具有焦点的日期时间选择器,而不是时间选择器。可能是多余的

【讨论】:

    【解决方案3】:

    我通过比较原始来源和来源解决了这个问题。

    e.OriginalSource == e.Source
    

    【讨论】: