Here is an example of disabling and enabling the close button.
MainWindow.xaml:
<Grid>
<ToggleButton x:Name="tb" Width="100" Height="40" Click="tb_Click">
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="enable"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Content" Value="disable"/>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
</Grid>
MainWindow.xaml.cs:
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Interop;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable);
const uint MF_GRAYED = 0x00000001;
const uint MF_ENABLED = 0x00000000;
const uint SC_CLOSE = 0xF060;
private void tb_Click(object sender, RoutedEventArgs e)
{
var tb = sender as ToggleButton;
if ((bool)tb.IsChecked)
{
var hwnd = new WindowInteropHelper(this).Handle;
IntPtr hMenu = GetSystemMenu(hwnd, false);
EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED);
}
else
{
var hwnd = new WindowInteropHelper(this).Handle;
IntPtr hMenu = GetSystemMenu(hwnd, false);
EnableMenuItem(hMenu, SC_CLOSE, MF_ENABLED);
}
}
}
The result:
Edit:
public partial class MainWindow : Window
{
ViewModel vm ;
public MainWindow()
{
InitializeComponent();
vm=new ViewModel();
DataContext=vm;
this.Closing += new CancelEventHandler(Window_Closing);
}
void Window_Closing(object sender, CancelEventArgs e)
{
e.Cancel = (this.DataContext as ViewModel).ProcessWorking;
}
private void btn_Click(object sender, RoutedEventArgs e)
{
var tb = sender as ToggleButton;
if ((bool)tb.IsChecked)
{
vm.ProcessWorking = true;
}
else
{
vm.ProcessWorking = false;
}
}
}
public class ViewModel:INotifyPropertyChanged
{
private bool processWorking=false;
public bool ProcessWorking
{
get { return this.processWorking; }
set
{
this.processWorking=value;
OnPropertyChanged("ProcessWorking");
}
}
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
public event PropertyChangedEventHandler PropertyChanged;
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.