简体   繁体   English

WPF值更改事件在值更改之前运行

[英]WPF value change event runs before value has changed

i have the following datepicker: 我有以下日期选择器:

<xctk:DateTimePicker HorizontalAlignment="Right" Margin="0,23,671,0" Name="datepicker" VerticalAlignment="Top" Width="120" Height="49" BorderBrush="Black" Cursor="Hand" ValueChanged="dateTimePicker1_ValueChanged" />

Now as you can see component has a valueChanged event. 现在,您可以看到组件具有valueChanged事件。 The event code looks like this: 事件代码如下所示:

    private void dateTimePicker1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{

    DateTime date = (DateTime)datepicker.Value;
    datepicker.Text = date.Date.ToString();
    UpdateDateLabels(date);
}

private void UpdateDateLabels(DateTime date) 
{
    dstart.Content = date.Date.ToShortDateString();

    switch (datebox.SelectedIndex) 
    {
        case 0:
            date.AddDays(1);
            break;
        case 1:
            date = date.AddDays(7);
            break;
        case 2:
            date = date.AddMonths(1);
            break;
        case 3:
            date = date.AddYears(1);
            break;
        default:
            break; 
    }
    MessageBox.Show(datebox.SelectedValue.ToString());
    dend.Content = date.Date.ToShortDateString();

}

Now as some of you might know a datepicker looks like this: 现在,您可能已经知道,日期选择器看起来像这样:

日期选择器

Now when i click the arrow keys (up and down) everything works as it should. 现在,当我单击箭头键(向上和向下)时,一切正常。 But if i however click the button to view the calendar then the changedevent is already fired without actually changing (i don't even get to see the calendar before i get the following error): 但是,如果我不过单击按钮以查看日历,则更改后的事件已经被触发而没有实际更改(在出现以下错误之前,我什至没有看到日历):

错误

Can anyone tell me why this is happening? 谁能告诉我为什么会这样吗?

You should check OriginalSource . 您应该检查OriginalSource

Try this: 尝试这个:

private void dateTimePicker1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
    if (e.OriginalSource is Xceed.Wpf.Toolkit.DateTimePicker)
    {
        DateTime date = (DateTime)datepicker.Value;
        datepicker.Text = date.Date.ToString();
        UpdateDateLabels(date);
    }
}

I'll go for the "teach a man to fish" answer. 我将寻求“教人钓鱼”的答案。

The best way to answer a "why is this happening?" 回答“为什么会这样?”的最佳方法 question is often to look at your call stack. 问题通常是看您的调用堆栈。 If it shows a line saying [] you [b]NEED[/b] to turn off Just My Code in the debugging options (Options->Debugging->Uncheck "Enable Just My Code". 如果显示一行,则在调试选项中(选项->调试->取消选中“启用我的代码”,请[b]需要[/ b]关闭我的代码)。

After doing this, you should see a much more informative call stack where the exception is occurring. 完成此操作后,您应该看到发生异常的更多信息的调用堆栈。

What I'm going to guess you'll see in there is a bunch of Binding calls, probably for the new editor that is being displayed when you click that button. 我想您会看到一堆Binding调用,可能是单击该按钮时正在显示的新编辑器。 There will often be value changed events thrown when the binding is established, even if the value has not changed. 建立绑定时,即使值没有更改,也经常会抛出值更改事件。

So, as per @kmatyaszek's answer, you need to be sure you're only handling the event when you need to. 因此,按照@kmatyaszek的回答,您需要确保仅在需要时才处理该事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM