wpfbutton按钮禁用_wpfbutton按钮禁用_如何在WPF窗口中隐藏关闭按钮?

我只是遇到了类似的问题,乔·怀特的解决方案在我看来似乎简单而干净。我重用了它并将其定义为Window的附加属性

public class WindowBehavior

{

private static readonly Type OwnerType = typeof (WindowBehavior);

#region HideCloseButton (attached property)

public static readonly DependencyProperty HideCloseButtonProperty =

DependencyProperty.RegisterAttached(

"HideCloseButton",

typeof (bool),

OwnerType,

new FrameworkPropertyMetadata(false, new PropertyChangedCallback(HideCloseButtonChangedCallback)));

[AttachedPropertyBrowsableForType(typeof(Window))]

public static bool GetHideCloseButton(Window obj) {

return (bool)obj.GetValue(HideCloseButtonProperty);

}

[AttachedPropertyBrowsableForType(typeof(Window))]

public static void SetHideCloseButton(Window obj, bool value) {

obj.SetValue(HideCloseButtonProperty, value);

}

private static void HideCloseButtonChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)

{

var window = d as Window;

if (window == null) return;

var hideCloseButton = (bool)e.NewValue;

if (hideCloseButton && !GetIsHiddenCloseButton(window)) {

if (!window.IsLoaded) {

window.Loaded += HideWhenLoadedDelegate;

}

else {

HideCloseButton(window);

}

SetIsHiddenCloseButton(window, true);

}

else if (!hideCloseButton && GetIsHiddenCloseButton(window)) {

if (!window.IsLoaded) {

window.Loaded -= ShowWhenLoadedDelegate;

}

else {

ShowCloseButton(window);

}

SetIsHiddenCloseButton(window, false);

}

}

#region Win32 imports

private const int GWL_STYLE = -16;

private const int WS_SYSMENU = 0x80000;

[DllImport("user32.dll", SetLastError = true)]

private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll")]

private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

#endregion

private static readonly RoutedEventHandler HideWhenLoadedDelegate = (sender, args) => {

if (sender is Window == false) return;

var w = (Window)sender;

HideCloseButton(w);

w.Loaded -= HideWhenLoadedDelegate;

};

private static readonly RoutedEventHandler ShowWhenLoadedDelegate = (sender, args) => {

if (sender is Window == false) return;

var w = (Window)sender;

ShowCloseButton(w);

w.Loaded -= ShowWhenLoadedDelegate;

};

private static void HideCloseButton(Window w) {

var hwnd = new WindowInteropHelper(w).Handle;

SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);

}

private static void ShowCloseButton(Window w) {

var hwnd = new WindowInteropHelper(w).Handle;

SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_SYSMENU);

}

#endregion

#region IsHiddenCloseButton (readonly attached property)

private static readonly DependencyPropertyKey IsHiddenCloseButtonKey =

DependencyProperty.RegisterAttachedReadOnly(

"IsHiddenCloseButton",

typeof (bool),

OwnerType,

new FrameworkPropertyMetadata(false));

public static readonly DependencyProperty IsHiddenCloseButtonProperty =

IsHiddenCloseButtonKey.DependencyProperty;

[AttachedPropertyBrowsableForType(typeof(Window))]

public static bool GetIsHiddenCloseButton(Window obj) {

return (bool)obj.GetValue(IsHiddenCloseButtonProperty);

}

private static void SetIsHiddenCloseButton(Window obj, bool value) {

obj.SetValue(IsHiddenCloseButtonKey, value);

}

#endregion

}

然后,在XAML中,您可以这样设置:

x:Class="WafClient.Presentation.Views.SampleWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:u="clr-namespace:WafClient.Presentation.Behaviors"

ResizeMode="NoResize"

u:WindowBehavior.HideCloseButton="True">

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值