WPF:是否MessageBox的休息previewMouseDown? [英] WPF: Does MessageBox Break PreviewMouseDown?

查看:241
本文介绍了WPF:是否MessageBox的休息previewMouseDown?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图让我的WPF应用程序提示用户是放弃自己的未保存的更改或者当他们浏览使用一个TreeView取消。

I've been trying to get my WPF application to prompt users to either discard their unsaved changes or to cancel when they navigate using a TreeView.

  • 如何取消用户的WPF TreeView的点击?
  • <一个href="http://stackoverflow.com/questions/543312/how-do-i-properly-handle-a-$p$pviewmousedown-event-with-a-messagebox-confirmation/545534#545534">How做我 妥善处理previewMouseDown 事件一个MessageBox 确认?

我想我已经找到了一个错误。在MessageBox没有发挥好与previewMouseDown。这似乎是处理点击不管如何,如果有显示一个消息的e.Handled设置。

I think I've found a bug. The MessageBox does not play nice with PreviewMouseDown. It seems to "handle" a click regardless of how its e.Handled is set if there's a MessageBox shown.

有关此XAML ...

For this XAML...

<TreeView Name="TreeViewThings"
    ...
    PreviewMouseDown="TreeViewThings_PreviewMouseDown"
    TreeViewItem.Expanded="TreeViewThings_Expanded"
    TreeViewItem.Selected="TreeViewThings_Selected" >

...比较这些替代方法...

...compare these alternative methods...


Sub TreeViewNodes_PreviewMouseDown(...)
    e.Handled = False
End Sub

Sub TreeViewNodes_PreviewMouseDown(...)
    MessageBox.Show("Test", "Test", MessageBoxButton.OK)
    e.Handled = False
End Sub

这两种方法的行为不同。如果没有消息框, TreeViewNodes_Selected() TreeViewThings_Expanded()将执行。随着消息框,他们不会。

These two methods behave differently. Without the MessageBox, TreeViewNodes_Selected() or TreeViewThings_Expanded() will execute. With the MessageBox, they won't.

这是一个bug或者是有什么事情在这里,我应该明白了吧?

Is this a bug or is there something going on here that I should understand?

推荐答案

我在precisely同样的问题,你是正确的思想,消息框搞砸的事情了。说实话,我有其他问题的MessageBox同时与Windows窗体的工作切换到WPF之前。也许它只是成为一个功能的一些百年老店的错误(因为通常情况下与微软)?

I'm having precisely the same problem and you're right in thinking that MessageBox is screwing things up. To be honest, I've had other issues with MessageBox while working with Windows Forms before switching to WPF. Maybe it's just some century-old bug that became a feature (as often it is with Microsoft)?

在任何情况下,唯一的解决办法,我可以给你的是,很适合我的人。我有问题越来越相似的情况下工作有一个ListBox - 如果有变化形式的数据,在选择列表框的变化(无论是通过单击新项目或使用按键向上或向下),我提供用户在MessageBox可以选择是否保存,放弃或取消。

In any case, the only solution I can offer you is the one that has worked for me. I was having problems with getting a similar situation to work with a ListBox - if there were changes to data in the form, when selection of the ListBox changed (either by clicking on new item or using keys "Up" or "Down"), I offered user a choice in the MessageBox whether to save, discard or cancel.

采用自然处理ListBox的的MouseDown或previewMouseDown事件并没有一个消息框做工精良的直接方法。这里是什么工作。

Naturally using the direct approach of handling ListBox's MouseDown or PreviewMouseDown events didn't work well with a MessageBox. Here's what worked.

我有一个数据模板来显示在我的列表框项目(我几乎希望你有相同):

I have a data template to display items in my ListBox (I'm almost expecting you to have the same):

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Path=NAME}" KeyDown="checkForChanges" MouseDown="checkForChanges"/>
    </DataTemplate>
</ListBox.ItemTemplate>

请注意我是如何移动KeyDown和MouseDown事件处理程序的TextBlock控件代替。我保留了相同的code-背后:

Note how I've moved the KeyDown and MouseDown event handlers to the TextBlock control instead. I kept the same code-behind:

// The KeyDown handler
private void checkForChanges(object sender, KeyEventArgs e) {
    e.Handled = checkForChanges();
}

// Method that checks if there are changes to be saved or discard or cancel
private bool checkForChanges() {
    if (Data.HasChanges()) {
        MessageBoxResult answer = MessageBox.Show("There are unsaved changes. Would you like to save changes now?", "WARNING", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
        if (answer == MessageBoxResult.Yes) {
            Data.AcceptDataChanges();
        } else if (answer == MessageBoxResult.Cancel) {
            return true;
        }
        return false;
    }
    return false;
}

// The MouseDown handler
private void checkForChanges(object sender, MouseButtonEventArgs e) {
    e.Handled = checkForChanges();
}

作为一个侧面说明,这是奇怪的是如何绑定总是​​标志着我的数据行的修改时,在列表框中选择的项目,已绑定的ItemsSource到数据表,修改(如果你使用的数据表我不知道/套) 。为了战斗,我放弃所有未处理的变化,一旦选择已经被改变(因为我处理任何必要的事件MouseDown在此之前发生的):

As a side note, it's odd how Binding always marks my DataRows as Modified when the selected item in the ListBox, which has ItemsSource bound to a DataTable, changes (I don't know if you're using DataTables/Sets). To battle that, I discard any unhandled changes once the selection has already been changed (because I handle anything that's necessary in the event MouseDown that occurs prior to that):

<ListBox IsSynchronizedWithCurrentItem="True" [...] SelectionChanged="clearChanges"> ... </ListBox>

和C-背后的$ C $的处理程序:

And the code-behind for the handler:

private void clearChanges(object sender, SelectionChangedEventArgs e) {
    Data.cancelChanges();
}

这篇关于WPF:是否MessageBox的休息previewMouseDown?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆