Marnix
Marnix

Reputation: 6547

Unclosable progressbar in WPF

I am looking for a forced ProgressBar that cannot be closed or canceled. I tried to make this window, but it can always be closed by ALT-F4.

I want to close the window when the process is finished.

<Window x:Class="BWCRenameUtility.View.BusyProgressBar"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="BusyProgressBar" WindowStyle="None" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterOwner" ResizeMode="NoResize">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Content="Exporting..."/>
        <ProgressBar Width="300" Height="20" IsIndeterminate="True" Grid.Row="1"/>
    </Grid>
</Window>

Upvotes: 0

Views: 609

Answers (2)

C&#233;dric Bignon
C&#233;dric Bignon

Reputation: 13022

You don't want a unclosable ProgressBar but un unclosable Window (a progress bar cannot be closed)!

To do this, use the event Window.Closing which occurs after a close request but before the effective close.

In your XAML:

<Window x:Class="BWCRenameUtility.View.BusyProgressBar"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="BusyProgressBar" WindowStyle="None" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterOwner" ResizeMode="NoResize"
        Closing="BusyProgressBar_OnClosing">

    <!-- Your code -->

</Window>

In the BusyProgressBar class, cancel the close request by setting CancelEventArgs.Cancel to true:

private void BusyProgressBar_OnClosing(object sender, CancelEventArgs e)
{
    e.Cancel = true;  // Cancels the close request
}

Update

Instead of using the event Window.Closing, an easier solution is to override Window.OnClosing.

protected override void OnClosing(CancelEventArgs e)
{
    e.Cancel = true;  // Cancels the close request
    base.OnClosing(e);
}

This way, you don't have to change anything to your XAML.

Upvotes: 3

Silberfab
Silberfab

Reputation: 65

Maybe just handle Closing event of the window and put e.Cancel = true

Upvotes: 0

Related Questions