开发者

MouseClicks reaching back window WPF

I have created a sort of "processing" dialogbox which displays the progess of a certain process.It contains one progressbar and one Label. Here is the code:

public class ProcessingDialog
    {
        FrontWindow win;
        public ProcessingDialog(int min, int max, string initialMessage,string title, bool isContineous)
        {
            Thread th= new Thread(new ThreadStart(delegate()
            {
                win = new FrontWindow();
                win.Title = title;
                if (isContineous == false)
                {
                    win.ProgressBarProgress.Minimum = min;
                    win.ProgressBarProgress.Maximum = max;
                }
                else
                    win.ProgressBarProgress.IsIndeterminate = true;

                win.LabelStatus.Content = initialMessage;
                win.ShowInTaskbar = false;
                win.Topmost = true;
                win.ShowDialog();
            }
            ));
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
        }


        public void Increment(int margin)
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.ProgressBarProgress.Value+=margin; }), null); ;
        }

        public void Reset()
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.ProgressBarProgress.Value=0; }), null); ;
        }

        public void ChangeMessage(string message)
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.LabelStatus.Content=message; }), null); ;
        }


        public void Close()
        {
            win.Dispatcher.Invoke(new ThreadStart(delegate { win.Close(); }), null); ;
        }
    }

This is the markup for FrontWindow:

<Window x:Class="WpfApplicationUnleashed.FrontWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="FrontWindow" Height="165" Width="300" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterOwner" ResizeMode="NoResize">
    <DockPanel>
        <ProgressBar x:FieldModifier="public" x:Name="ProgressBarProgress" Height="15" Margin="20" DockPanel.Dock="Top" VerticalAlignment="Top"></ProgressBar>
        <Label x:FieldModifier="public" x:Name="LabelStatus" Margin="10" VerticalAlignment="Top" HorizontalAlignment="Left" DockPanel.Dock="Bottom"></Label>
    </DockPanel>
</Window>

And here is the Window from where I show the dialogbox.It just cantains one buttons whose click event is this:

   private void button1_Click(object sender, RoutedEventArgs e)
        {
            ProcessingDialog pd = new ProcessingDialog(0, 10, "Starting", "Processing", false);
            Thread.Sleep(1000);

            pd.ChangeMessage("Step 1");
            pd.Increment(1);
            Thread.Sleep(1000);

            pd.ChangeMessage("Step 2");
            pd.Increment(1);
            Thread.Sleep(1000);开发者_开发知识库

            pd.ChangeMessage("Step 3");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 4");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 5");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 6");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 7");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 8");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 9");
            pd.Increment(1);
            Thread.Sleep(1000);


            pd.ChangeMessage("Step 10");
            pd.Increment(1);
            Thread.Sleep(1000);

            pd.Close();


        }

The problem is that when it's being shown, if i click the button again, when the dialogbox is closed, it opens again. I mean it does not get clicked because it's Modal, but it just queues up and when the dialogbox is closed, it gets clicked.

How can I prevent this from happening?


Maybe bind a command to the button and write a CanExecute method which prevents the command from being executed if it is already in progress.


If the back window is not meant to be used while the front window is open, I would set an event on the frontwindow's loaded to disable the back window, and on close, enable the back window.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜