更新
This commit is contained in:
153
WPFDark/Controls/Internals/BiaSystemButton.xaml.cs
Normal file
153
WPFDark/Controls/Internals/BiaSystemButton.xaml.cs
Normal file
@@ -0,0 +1,153 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace WPFDark.Controls.Internals
|
||||
{
|
||||
internal class BiaSystemButton : Button
|
||||
{
|
||||
#region BiaWindowAction
|
||||
|
||||
public BiaWindowAction WindowAction
|
||||
{
|
||||
get => windowAction;
|
||||
set
|
||||
{
|
||||
if (value != windowAction)
|
||||
SetValue(WindowActionProperty, value);
|
||||
}
|
||||
}
|
||||
|
||||
private BiaWindowAction windowAction;
|
||||
|
||||
public static readonly DependencyProperty WindowActionProperty =
|
||||
DependencyProperty.Register(nameof(WindowAction), typeof(BiaWindowAction), typeof(BiaSystemButton),
|
||||
new PropertyMetadata(
|
||||
BiaWindowAction.None,
|
||||
(s, e) =>
|
||||
{
|
||||
var self = (BiaSystemButton)s;
|
||||
self.windowAction = (BiaWindowAction)e.NewValue;
|
||||
}));
|
||||
|
||||
#endregion
|
||||
|
||||
#region IsVisibleButton
|
||||
|
||||
public bool IsVisibleButton
|
||||
{
|
||||
get => _IsVisibleButton;
|
||||
set
|
||||
{
|
||||
if (value != _IsVisibleButton)
|
||||
SetValue(IsVisibleButtonProperty, value);
|
||||
}
|
||||
}
|
||||
|
||||
private bool _IsVisibleButton = true;
|
||||
|
||||
public static readonly DependencyProperty IsVisibleButtonProperty =
|
||||
DependencyProperty.Register(
|
||||
nameof(IsVisibleButton),
|
||||
typeof(bool),
|
||||
typeof(BiaSystemButton),
|
||||
new PropertyMetadata(
|
||||
true,
|
||||
(s, e) =>
|
||||
{
|
||||
var self = (BiaSystemButton)s;
|
||||
self._IsVisibleButton = (bool)e.NewValue;
|
||||
|
||||
self.MakeVisibility();
|
||||
}));
|
||||
|
||||
#endregion
|
||||
|
||||
static BiaSystemButton()
|
||||
{
|
||||
DefaultStyleKeyProperty.OverrideMetadata(typeof(BiaSystemButton),
|
||||
new FrameworkPropertyMetadata(typeof(BiaSystemButton)));
|
||||
}
|
||||
|
||||
private BiaWindow? parentWindow;
|
||||
|
||||
protected override void OnInitialized(EventArgs e)
|
||||
{
|
||||
base.OnInitialized(e);
|
||||
|
||||
parentWindow = (BiaWindow)Window.GetWindow(this);
|
||||
|
||||
if (parentWindow != null)
|
||||
parentWindow.StateChanged += ParentWindowOnStateChanged;
|
||||
|
||||
MakeVisibility();
|
||||
}
|
||||
|
||||
private void ParentWindowOnStateChanged(object? sender, EventArgs e)
|
||||
{
|
||||
MakeVisibility();
|
||||
}
|
||||
|
||||
protected override void OnClick()
|
||||
{
|
||||
base.OnClick();
|
||||
|
||||
if (parentWindow is null)
|
||||
return;
|
||||
|
||||
switch (WindowAction)
|
||||
{
|
||||
case BiaWindowAction.None:
|
||||
break;
|
||||
|
||||
case BiaWindowAction.Active:
|
||||
parentWindow.Activate();
|
||||
break;
|
||||
|
||||
case BiaWindowAction.Close:
|
||||
if (parentWindow.CloseButtonBehavior == BiaWindowCloseButtonBehavior.Normal)
|
||||
parentWindow.Close();
|
||||
|
||||
parentWindow.InvokeCloseButtonClicked();
|
||||
|
||||
break;
|
||||
|
||||
case BiaWindowAction.Maximize:
|
||||
parentWindow.WindowState = WindowState.Maximized;
|
||||
break;
|
||||
|
||||
case BiaWindowAction.Minimize:
|
||||
parentWindow.WindowState = WindowState.Minimized;
|
||||
break;
|
||||
|
||||
case BiaWindowAction.Normalize:
|
||||
parentWindow.WindowState = WindowState.Normal;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 控制按钮显隐
|
||||
/// </summary>
|
||||
private void MakeVisibility()
|
||||
{
|
||||
if (IsVisibleButton == false)
|
||||
{
|
||||
Visibility = Visibility.Collapsed;
|
||||
return;
|
||||
}
|
||||
|
||||
if (parentWindow is null)
|
||||
return;
|
||||
|
||||
Visibility = WindowAction switch
|
||||
{
|
||||
BiaWindowAction.Maximize => parentWindow.WindowState != WindowState.Maximized ? Visibility.Visible : Visibility.Collapsed,
|
||||
BiaWindowAction.Minimize => parentWindow.WindowState != WindowState.Minimized ? Visibility.Visible : Visibility.Collapsed,
|
||||
BiaWindowAction.Normalize => parentWindow.WindowState != WindowState.Normal ? Visibility.Visible : Visibility.Collapsed,
|
||||
_ => Visibility
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user