1

I am trying to disable the close button on a window via MVVM

I realise that you can do this in the view (window) CS code by stating

public Window()
{
    InitializeComponent();
    this.Closing += new System.ComponentModel.CancelEventHandler(Window_Closing);
}

void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = true;
}

However I would like to keep it consistent and try to do this is the MVVM.

Thanks

daniele3004
  • 13,072
  • 12
  • 67
  • 75
user3428422
  • 4,300
  • 12
  • 55
  • 119
  • 1
    why you do it like that? You have a close button and you disable it's function,so, why not make it hidden? – Rang Jun 24 '14 at 06:50
  • Just because I am firing a background thread so I need to stop the user from firing events as the hread is happening, once the thread has finished, of couse I will enable it back – user3428422 Jun 24 '14 at 07:19

3 Answers3

2

It's a strange demand. If you have a closing button,why you disable it's func. But you can realize it with mvvm like this:

add two ref: - Microsoft.Expression.Interactions.dll - System.Windows.Interactivity.dll

add two xmlns:

 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

create trigger to window:

 <Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:control="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        Title="MainWindow" Height="350" Width="525">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Closing">
            <ei:CallMethodAction TargetObject="{Binding}" MethodName="WindowsClosing"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid >

    </Grid>
</Window>

edit viewmodel,and creat closing func:

 public void WindowsClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }
Rang
  • 1,362
  • 7
  • 17
  • 30
  • Thanks this looks good. Will and try and mark as answer sometime today. Its because I am firing a background thread so I need to stop the user from firing events as the thread is happening, once the thread has finished, of couse I will enable it back – user3428422 Jun 24 '14 at 07:20
  • @user3428422 I see now! btw , Interaction need .net4.0 at least. – Rang Jun 24 '14 at 07:29
0

you can use the ResizeMode of Window or you can use it by using Window API use of Window API mentioend Here

Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70
0

Change your Closing method with a variable from ViewModel.

void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = (this.DataContext as MyViewModel).ProcessWorking;
}

In your ViewModel (MyViewModel) add a property ProcessWorking :

public Boolean ProcessWorking
{
    get { return this.processWorking; }
}

and in your method of background thread, just modify processWorking

private Boolean processWorking;

private void MyBackgroundThread()
{
    this.processWorking = true;

    // do your process

    this.processWorking = false;
}

You can add a RaisePropertyChange() when you modify this.processWorking if you want to show somewhere of your UI the state of the background process.

Xaruth
  • 4,034
  • 3
  • 19
  • 26