如何在TabItem更改之前使Tabcontrol弹出一条消息

时间:2014-07-28 09:13:21

标签: c# silverlight tabcontrol tabitem selectionchanged

我在更改TabItem的情况下使用TabControl的Selectionchanged事件。

我这样做:

tabcntrl.SelectionChanged += new SelectionChangedEventHandler( obj.TabControl_SelectionChanged);
public void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MessageBox.Show("check1");
}

我想要的是当我单击要更改的选项卡然后在打开选项卡之前它应该打印此messageBox并且“它与我编写的代码完美匹配”。 但是,当我将此代码更改为:(我的coustom按钮由childwindow制作)

tabcntrl.SelectionChanged += new SelectionChangedEventHandler( obj.TabControl_SelectionChanged);
 public void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           string msg = "Tab Changed :Save data of MetaviewModel ?";
           CoustomButtonMessage cusButton = new CoustomButtonMessage("Error", msg, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error);
           cusButton.Show();
        }

此代码首先更改选项卡 :切换到另一个选项卡,然后它弹出此消息框,而在以前的代码中,它首先弹出meassgebox然后切换到另一个 tab.And我想要的是让第二个代码像第一个代码一样工作。

我的意思是它应该首先弹出第二个代码的消息框然后它应该切换到另一个不是之前的选项卡。 (这是因为这个弹出窗口将再次决定保存当前标签的数据,或者不进入下一个标签)

How to acheieve this ?

2 个答案:

答案 0 :(得分:1)

问题是ChildWindow.Show()似乎是半模式的,这意味着它会立即返回,但是当窗口实际打开时,它表现为模态窗口 - 即背景中的所有内容都是灰色的。 要解决此问题,您必须订阅ChildWindow的Closed事件,并仅在触发此事件时执行自定义操作。

在这里,我来告诉你如何做到这一点:

private void Button_Click(object sender, RoutedEventArgs e)
{
    ChildWindow w = new ChildWindow();
    ShowChildWindowAndPerformAction(w,
        () =>
        {
            //put your custom code here that needs to be run when the window is closed
            int i = 5;
        });
}

public void ShowChildWindowAndPerformAction(ChildWindow w, Action action)
{
    EventHandler handler = null;
    handler = (sender2, args2) =>
    {
        w.Closed -= handler;

        action();
    };

    w.Closed += handler;

    w.Show();
}

答案 1 :(得分:0)

也许您可以尝试以低线程优先级异步显示消息框,

Dispatcher.InvokeAsync(()=>MessageBox.Show("Test"), DispatcherPriority.Background);
相关问题