From 95f7b76a03dac0200c6897dd219f21b44d3d488a Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Mon, 30 Sep 2024 23:17:10 +0800 Subject: [PATCH] add TwoStateButton --- .../View/Controls/TwoStateButton.cs | 92 +++++++++++++++ BetterGenshinImpact/View/Pages/HomePage.xaml | 23 ++-- .../View/Pages/TaskSettingsPage.xaml | 109 ++++++++++-------- 3 files changed, 166 insertions(+), 58 deletions(-) create mode 100644 BetterGenshinImpact/View/Controls/TwoStateButton.cs diff --git a/BetterGenshinImpact/View/Controls/TwoStateButton.cs b/BetterGenshinImpact/View/Controls/TwoStateButton.cs new file mode 100644 index 00000000..ae8468b3 --- /dev/null +++ b/BetterGenshinImpact/View/Controls/TwoStateButton.cs @@ -0,0 +1,92 @@ +using System.Windows; +using System.Windows.Input; +using Wpf.Ui.Controls; + +namespace BetterGenshinImpact.View.Controls; + +public class TwoStateButton : Button +{ + private bool _isEnabled; + + public TwoStateButton() + { + if (TryFindResource(typeof(Button)) is Style style) + { + Style = style; + } + + Click += OnClick; + Loaded += OnLoaded; + } + + private void OnLoaded(object sender, RoutedEventArgs e) + { + Content = EnableContent; + Icon = EnableIcon; + } + + private void OnClick(object sender, RoutedEventArgs e) + { + if (_isEnabled) + { + DisableCommand?.Execute(null); + Content = EnableContent; + Icon = EnableIcon; + } + else + { + EnableCommand?.Execute(null); + Content = DisableContent; + Icon = DisableIcon; + } + _isEnabled = !_isEnabled; + } + + private static readonly DependencyProperty EnableContentProperty = DependencyProperty.Register(nameof(EnableContent), typeof(object), typeof(TwoStateButton), new PropertyMetadata("启动")); + + public object EnableContent + { + get => (string)GetValue(EnableContentProperty); + set => SetValue(EnableContentProperty, value); + } + + private static readonly DependencyProperty EnableIconProperty = DependencyProperty.Register(nameof(EnableIcon), typeof(IconElement), typeof(TwoStateButton), new PropertyMetadata(null)); + + public IconElement EnableIcon + { + get => (IconElement)GetValue(EnableIconProperty); + set => SetValue(EnableIconProperty, value); + } + + private static readonly DependencyProperty EnableCommandProperty = DependencyProperty.Register(nameof(EnableCommand), typeof(ICommand), typeof(TwoStateButton), new PropertyMetadata(null)); + + public ICommand EnableCommand + { + get => (ICommand)GetValue(EnableCommandProperty); + set => SetValue(EnableCommandProperty, value); + } + + private static readonly DependencyProperty DisableContentProperty = DependencyProperty.Register(nameof(DisableContent), typeof(object), typeof(TwoStateButton), new PropertyMetadata("停止")); + + public object DisableContent + { + get => (string)GetValue(DisableContentProperty); + set => SetValue(DisableContentProperty, value); + } + + private static readonly DependencyProperty DisableIconProperty = DependencyProperty.Register(nameof(DisableIcon), typeof(IconElement), typeof(TwoStateButton), new PropertyMetadata(null)); + + public IconElement DisableIcon + { + get => (IconElement)GetValue(DisableIconProperty); + set => SetValue(DisableIconProperty, value); + } + + private static readonly DependencyProperty DisableCommandProperty = DependencyProperty.Register(nameof(DisableCommand), typeof(ICommand), typeof(TwoStateButton), new PropertyMetadata(null)); + + public ICommand DisableCommand + { + get => (ICommand)GetValue(DisableCommandProperty); + set => SetValue(DisableCommandProperty, value); + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/View/Pages/HomePage.xaml b/BetterGenshinImpact/View/Pages/HomePage.xaml index 4d89a677..e476842a 100644 --- a/BetterGenshinImpact/View/Pages/HomePage.xaml +++ b/BetterGenshinImpact/View/Pages/HomePage.xaml @@ -2,6 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:b="http://schemas.microsoft.com/xaml/behaviors" + xmlns:controls="clr-namespace:BetterGenshinImpact.View.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:BetterGenshinImpact.View.Pages" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" @@ -95,14 +96,20 @@ Grid.Column="1" Margin="0,0,24,0" Orientation="Horizontal"> - - + + + + + + + + + diff --git a/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml b/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml index 85388868..f22c5008 100644 --- a/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml +++ b/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml @@ -2,6 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:b="http://schemas.microsoft.com/xaml/behaviors" + xmlns:controls="clr-namespace:BetterGenshinImpact.View.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:helpers="clr-namespace:BetterGenshinImpact.Helpers" xmlns:local="clr-namespace:BetterGenshinImpact.View.Pages" @@ -23,32 +24,32 @@ - - - - - - - - - - - 停止下方独立任务的运行 - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -78,12 +79,14 @@ 点击查看使用教程 - + @@ -178,12 +181,14 @@ 点击查看使用教程 - + @@ -346,12 +351,14 @@ 点击查看使用教程 - + @@ -504,12 +511,14 @@ 点击查看使用教程 - +