From 9fffaaaa95ccc0321272de8b89e70d9be71dbb1f Mon Sep 17 00:00:00 2001 From: Hakoyu Date: Sat, 30 Dec 2023 23:50:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20`VPet.Solution`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VPet-Simulator.Core/Display/Theme.xaml | 71 +- VPet-Simulator.Core/Display/basestyle.xaml | 635 ++++++++++-------- .../ResourceStyle.xaml | 335 ++++----- VPet-Simulator.Windows/App.xaml | 54 +- VPet.Solution/Converters.xaml | 11 +- .../Converters/AllIsBoolConverter.cs | 52 ++ .../AllIsBoolToVisibilityConverter.cs | 97 +++ VPet.Solution/Converters/BoolInverter.cs | 42 +- .../Converters/BoolToVisibilityConverter.cs | 63 ++ .../Converters/BrushToMediaColorConverter.cs | 31 +- .../Converters/CalculatorConverter.cs | 31 +- .../ConverterBase/BoolToValueConverterBase.cs | 47 ++ .../CanInverterMultiValueConverter.cs | 31 + .../CanInverterValueConverter.cs | 31 + .../Converters/ConverterBase/ConverterBase.cs | 15 + .../ConverterBase/HaveRatioConverter.cs | 32 + .../ConverterBase/MultiValueConverterBase.cs | 45 ++ .../MultiValueToBoolConverter.cs | 31 + .../ConverterBase/ValueConverterBase.cs | 45 ++ VPet.Solution/Converters/EqualsConverter.cs | 41 +- .../Converters/FalseToCollapsedConverter.cs | 20 - .../Converters/FalseToHiddenConverter.cs | 20 - VPet.Solution/Converters/IsBoolConverter.cs | 62 ++ VPet.Solution/Converters/MarginConverter.cs | 156 +++-- VPet.Solution/Converters/MaxConverter.cs | 18 +- .../Converters/MediaColorToBrushConverter.cs | 47 +- .../Converters/NotEqualsConverter.cs | 28 - .../Converters/NullToFalseConverter.cs | 17 - .../Converters/RatioMarginConverter.cs | 91 --- .../Converters/StringFormatConverter.cs | 30 +- .../Converters/ValueToBoolConverter.cs | 22 + VPet.Solution/Utils/Utils.cs | 12 + VPet.Solution/VPet.Solution.csproj | 32 +- 33 files changed, 1413 insertions(+), 882 deletions(-) create mode 100644 VPet.Solution/Converters/AllIsBoolConverter.cs create mode 100644 VPet.Solution/Converters/AllIsBoolToVisibilityConverter.cs create mode 100644 VPet.Solution/Converters/BoolToVisibilityConverter.cs create mode 100644 VPet.Solution/Converters/ConverterBase/BoolToValueConverterBase.cs create mode 100644 VPet.Solution/Converters/ConverterBase/CanInverterMultiValueConverter.cs create mode 100644 VPet.Solution/Converters/ConverterBase/CanInverterValueConverter.cs create mode 100644 VPet.Solution/Converters/ConverterBase/ConverterBase.cs create mode 100644 VPet.Solution/Converters/ConverterBase/HaveRatioConverter.cs create mode 100644 VPet.Solution/Converters/ConverterBase/MultiValueConverterBase.cs create mode 100644 VPet.Solution/Converters/ConverterBase/MultiValueToBoolConverter.cs create mode 100644 VPet.Solution/Converters/ConverterBase/ValueConverterBase.cs delete mode 100644 VPet.Solution/Converters/FalseToCollapsedConverter.cs delete mode 100644 VPet.Solution/Converters/FalseToHiddenConverter.cs create mode 100644 VPet.Solution/Converters/IsBoolConverter.cs delete mode 100644 VPet.Solution/Converters/NotEqualsConverter.cs delete mode 100644 VPet.Solution/Converters/NullToFalseConverter.cs delete mode 100644 VPet.Solution/Converters/RatioMarginConverter.cs create mode 100644 VPet.Solution/Converters/ValueToBoolConverter.cs diff --git a/VPet-Simulator.Core/Display/Theme.xaml b/VPet-Simulator.Core/Display/Theme.xaml index a3d43df..32e6bd5 100644 --- a/VPet-Simulator.Core/Display/Theme.xaml +++ b/VPet-Simulator.Core/Display/Theme.xaml @@ -1,42 +1,43 @@ - - - - - - + + + + + + - - - - - + + + + + - - - - - + + + + + - - - - - + + + + + - - - - - + + + + + - - - - - + + + + + - #90caf9 + #90caf9 \ No newline at end of file diff --git a/VPet-Simulator.Core/Display/basestyle.xaml b/VPet-Simulator.Core/Display/basestyle.xaml index fcad8b7..3a42767 100644 --- a/VPet-Simulator.Core/Display/basestyle.xaml +++ b/VPet-Simulator.Core/Display/basestyle.xaml @@ -1,307 +1,364 @@ - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - + + + + - + - + - - - + - + - + - + - + - + - - - - - - + - + + + + + + - + - + + + + + + \ No newline at end of file diff --git a/VPet-Simulator.Windows.Interface/ResourceStyle.xaml b/VPet-Simulator.Windows.Interface/ResourceStyle.xaml index ae97856..8a3c64c 100644 --- a/VPet-Simulator.Windows.Interface/ResourceStyle.xaml +++ b/VPet-Simulator.Windows.Interface/ResourceStyle.xaml @@ -1,212 +1,139 @@ - - - - - + + + + + - - - - - - - - - - - + + + + - + + + + - - - + + + + + + - + + + + + \ No newline at end of file diff --git a/VPet-Simulator.Windows/App.xaml b/VPet-Simulator.Windows/App.xaml index 9d1b4f7..c7f2e01 100644 --- a/VPet-Simulator.Windows/App.xaml +++ b/VPet-Simulator.Windows/App.xaml @@ -1,29 +1,31 @@ - - + + + + + - - - - - /VPet-Simulator.Windows;component/Res/Font/#OPPOSans R - - - /VPet-Simulator.Windows;component/Res/#remixicon - - - - - - + + /VPet-Simulator.Windows;component/Res/Font/#OPPOSans R + + + /VPet-Simulator.Windows;component/Res/#remixicon + - + + + + + + diff --git a/VPet.Solution/Converters.xaml b/VPet.Solution/Converters.xaml index 45ff5da..33e24a6 100644 --- a/VPet.Solution/Converters.xaml +++ b/VPet.Solution/Converters.xaml @@ -1,16 +1,15 @@  + xmlns:c="clr-namespace:HKW.WPF.Converters"> - + - - + - - + + \ No newline at end of file diff --git a/VPet.Solution/Converters/AllIsBoolConverter.cs b/VPet.Solution/Converters/AllIsBoolConverter.cs new file mode 100644 index 0000000..80a9a09 --- /dev/null +++ b/VPet.Solution/Converters/AllIsBoolConverter.cs @@ -0,0 +1,52 @@ +using System.ComponentModel; +using System.Globalization; +using System.Windows; + +namespace HKW.WPF.Converters; + +/// +/// 全部为布尔值转换器 +/// +public class AllIsBoolConverter : MultiValueToBoolConverter +{ + /// + /// + /// + public static readonly DependencyProperty BoolOnNullProperty = DependencyProperty.Register( + nameof(BoolOnNull), + typeof(bool), + typeof(AllIsBoolConverter), + new PropertyMetadata(false) + ); + + /// + /// 目标为空时的指定值 + /// + [DefaultValue(false)] + public bool BoolOnNull + { + get => (bool)GetValue(BoolOnNullProperty); + set => SetValue(BoolOnNullProperty, value); + } + + /// + /// + /// + /// + /// + /// + /// + /// + /// + public override object Convert( + object[] values, + Type targetType, + object parameter, + CultureInfo culture + ) + { + var boolValue = TargetBoolValue; + var nullValue = BoolOnNull; + return values.All(o => Utils.GetBool(o, boolValue, nullValue)); + } +} diff --git a/VPet.Solution/Converters/AllIsBoolToVisibilityConverter.cs b/VPet.Solution/Converters/AllIsBoolToVisibilityConverter.cs new file mode 100644 index 0000000..b2ec4c9 --- /dev/null +++ b/VPet.Solution/Converters/AllIsBoolToVisibilityConverter.cs @@ -0,0 +1,97 @@ +using System.ComponentModel; +using System.Globalization; +using System.Windows; + +namespace HKW.WPF.Converters; + +/// +/// 全部为布尔值转换器 +/// +public class AllIsBoolToVisibilityConverter + : MultiValueToBoolConverter +{ + /// + /// + /// + public static readonly DependencyProperty BoolOnNullProperty = DependencyProperty.Register( + nameof(BoolOnNull), + typeof(bool), + typeof(AllIsBoolToVisibilityConverter), + new PropertyMetadata(false) + ); + + /// + /// 目标为空时的指定值 + /// + [DefaultValue(false)] + public bool BoolOnNull + { + get => (bool)GetValue(BoolOnNullProperty); + set => SetValue(BoolOnNullProperty, value); + } + + /// + /// + /// + public static readonly DependencyProperty VisibilityOnTrueProperty = + DependencyProperty.Register( + nameof(VisibilityOnTrue), + typeof(Visibility), + typeof(AllIsBoolToVisibilityConverter), + new PropertyMetadata(Visibility.Visible) + ); + + /// + /// 目标为空时的指定值 + /// + [DefaultValue(Visibility.Visible)] + public Visibility VisibilityOnTrue + { + get => (Visibility)GetValue(TargetBoolValueProperty); + set => SetValue(TargetBoolValueProperty, value); + } + + /// + /// + /// + public static readonly DependencyProperty VisibilityOnFalseProperty = + DependencyProperty.Register( + nameof(VisibilityOnFalse), + typeof(Visibility), + typeof(AllIsBoolToVisibilityConverter), + new PropertyMetadata(Visibility.Hidden) + ); + + /// + /// 目标为空时的指定值 + /// + [DefaultValue(Visibility.Hidden)] + public Visibility VisibilityOnFalse + { + get => (Visibility)GetValue(TargetBoolValueProperty); + set => SetValue(TargetBoolValueProperty, value); + } + + /// + /// + /// + /// + /// + /// + /// + /// + /// + public override object Convert( + object[] values, + Type targetType, + object parameter, + CultureInfo culture + ) + { + var boolValue = TargetBoolValue; + var nullValue = BoolOnNull; + return values.All(o => Utils.GetBool(o, boolValue, nullValue)) + ? VisibilityOnTrue + : VisibilityOnFalse; + } +} diff --git a/VPet.Solution/Converters/BoolInverter.cs b/VPet.Solution/Converters/BoolInverter.cs index d8a5e55..7ab221f 100644 --- a/VPet.Solution/Converters/BoolInverter.cs +++ b/VPet.Solution/Converters/BoolInverter.cs @@ -1,19 +1,41 @@ -using System.Globalization; +using System.ComponentModel; +using System.Globalization; +using System.Windows; using System.Windows.Data; -namespace VPet.House.Converters; +namespace HKW.WPF.Converters; -public class BoolInverter : IValueConverter +public class BoolInverter : ValueConverterBase { - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + /// + /// + /// + public static readonly DependencyProperty NullValueProperty = DependencyProperty.Register( + nameof(NullValue), + typeof(bool), + typeof(AllIsBoolToVisibilityConverter), + new PropertyMetadata(false) + ); + + /// + /// 为空值时布尔值 + /// + [DefaultValue(false)] + public bool NullValue + { + get => (bool)GetValue(NullValueProperty); + set => SetValue(NullValueProperty, value); + } + + public override object Convert( + object value, + Type targetType, + object parameter, + CultureInfo culture + ) { if (value is not bool b) - return false; + return NullValue; return !b; } - - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - throw new NotImplementedException(); - } } diff --git a/VPet.Solution/Converters/BoolToVisibilityConverter.cs b/VPet.Solution/Converters/BoolToVisibilityConverter.cs new file mode 100644 index 0000000..9c59821 --- /dev/null +++ b/VPet.Solution/Converters/BoolToVisibilityConverter.cs @@ -0,0 +1,63 @@ +using System.ComponentModel; +using System.Globalization; +using System.Windows; +using System.Windows.Data; + +namespace HKW.WPF.Converters; + +public class BoolToVisibilityConverter : BoolToValueConverterBase +{ + /// + /// + /// + public static readonly DependencyProperty TrueVisibilityValueProperty = + DependencyProperty.Register( + nameof(TrueVisibilityValue), + typeof(Visibility), + typeof(AllIsBoolToVisibilityConverter), + new PropertyMetadata(Visibility.Visible) + ); + + /// + /// 为真时的可见度 + /// + [DefaultValue(Visibility.Visible)] + public Visibility TrueVisibilityValue + { + get => (Visibility)GetValue(TrueVisibilityValueProperty); + set => SetValue(TrueVisibilityValueProperty, value); + } + + /// + /// + /// + public static readonly DependencyProperty FalseVisibilityValueProperty = + DependencyProperty.Register( + nameof(FalseVisibilityValue), + typeof(Visibility), + typeof(AllIsBoolToVisibilityConverter), + new PropertyMetadata(Visibility.Hidden) + ); + + /// + /// 为假时的可见度 + /// + [DefaultValue(Visibility.Hidden)] + public Visibility FalseVisibilityValue + { + get => (Visibility)GetValue(FalseVisibilityValueProperty); + set => SetValue(FalseVisibilityValueProperty, value); + } + + public override object Convert( + object value, + Type targetType, + object parameter, + CultureInfo culture + ) + { + return Utils.GetBool(value, BoolValue, NullValue) + ? TrueVisibilityValue + : FalseVisibilityValue; + } +} diff --git a/VPet.Solution/Converters/BrushToMediaColorConverter.cs b/VPet.Solution/Converters/BrushToMediaColorConverter.cs index 773e095..7597d5c 100644 --- a/VPet.Solution/Converters/BrushToMediaColorConverter.cs +++ b/VPet.Solution/Converters/BrushToMediaColorConverter.cs @@ -1,22 +1,39 @@ using System.Globalization; -using System.Windows.Data; using System.Windows.Media; -namespace VPet.House.Converters; +namespace HKW.WPF.Converters; -public class BrushToMediaColorConverter : IValueConverter +/// +/// 画笔颜色至媒体颜色转换器 +/// +public class BrushToMediaColorConverter : ValueConverterBase { - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + /// + public override object Convert( + object value, + Type targetType, + object parameter, + CultureInfo culture + ) { if (value is not SolidColorBrush brush) - throw new ArgumentException("Not SolidColorBrush", nameof(value)); + throw new ArgumentException( + $"Not type: {typeof(SolidColorBrush).FullName}", + nameof(value) + ); return brush.Color; } - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + /// + public override object ConvertBack( + object value, + Type targetType, + object parameter, + CultureInfo culture + ) { if (value is not Color color) - throw new ArgumentException("Not media color", nameof(value)); + throw new ArgumentException($"Not type: {typeof(Color).FullName}", nameof(value)); return new SolidColorBrush(color); } } diff --git a/VPet.Solution/Converters/CalculatorConverter.cs b/VPet.Solution/Converters/CalculatorConverter.cs index 0bfab22..7590d99 100644 --- a/VPet.Solution/Converters/CalculatorConverter.cs +++ b/VPet.Solution/Converters/CalculatorConverter.cs @@ -1,8 +1,7 @@ using System.Globalization; using System.Windows; -using System.Windows.Data; -namespace VPet.House.Converters; +namespace HKW.WPF.Converters; /// /// 计算器转换器 @@ -30,9 +29,15 @@ namespace VPet.House.Converters; /// ]]> /// /// 绑定的数量不正确 -public class CalculatorConverter : IMultiValueConverter +public class CalculatorConverter : MultiValueConverterBase { - public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) + /// + public override object Convert( + object[] values, + Type targetType, + object parameter, + CultureInfo culture + ) { if (values.Any(i => i == DependencyProperty.UnsetValue)) return 0.0; @@ -72,6 +77,14 @@ public class CalculatorConverter : IMultiValueConverter return result; } + /// + /// 计算 + /// + /// 值1 + /// 符号 + /// 值2 + /// 结果 + /// public static double Operation(double value1, char operatorChar, double value2) { return operatorChar switch @@ -84,14 +97,4 @@ public class CalculatorConverter : IMultiValueConverter _ => throw new NotImplementedException(), }; } - - public object[] ConvertBack( - object value, - Type[] targetTypes, - object parameter, - CultureInfo culture - ) - { - throw new NotImplementedException(); - } } diff --git a/VPet.Solution/Converters/ConverterBase/BoolToValueConverterBase.cs b/VPet.Solution/Converters/ConverterBase/BoolToValueConverterBase.cs new file mode 100644 index 0000000..94ebd02 --- /dev/null +++ b/VPet.Solution/Converters/ConverterBase/BoolToValueConverterBase.cs @@ -0,0 +1,47 @@ +using System.ComponentModel; +using System.Windows; + +namespace HKW.WPF.Converters; + +public abstract class BoolToValueConverterBase : ValueConverterBase +{ + /// + /// + /// + public static readonly DependencyProperty BoolValueProperty = DependencyProperty.Register( + nameof(BoolValue), + typeof(bool), + typeof(T), + new PropertyMetadata(true) + ); + + /// + /// 目标布尔值 + /// + [DefaultValue(true)] + public bool BoolValue + { + get => (bool)GetValue(BoolValueProperty); + set => SetValue(BoolValueProperty, value); + } + + /// + /// + /// + public static readonly DependencyProperty NullValueProperty = DependencyProperty.Register( + nameof(NullValue), + typeof(bool), + typeof(T), + new PropertyMetadata(false) + ); + + /// + /// 为空值时布尔值 + /// + [DefaultValue(false)] + public bool NullValue + { + get => (bool)GetValue(NullValueProperty); + set => SetValue(NullValueProperty, value); + } +} diff --git a/VPet.Solution/Converters/ConverterBase/CanInverterMultiValueConverter.cs b/VPet.Solution/Converters/ConverterBase/CanInverterMultiValueConverter.cs new file mode 100644 index 0000000..7736882 --- /dev/null +++ b/VPet.Solution/Converters/ConverterBase/CanInverterMultiValueConverter.cs @@ -0,0 +1,31 @@ +using System.ComponentModel; +using System.Windows; + +namespace HKW.WPF.Converters; + +/// +/// 可反转值转换器 +/// +public abstract class CanInverterMultiValueConverter : MultiValueConverterBase + where T : CanInverterMultiValueConverter +{ + /// + /// + /// + public static readonly DependencyProperty InverterProperty = DependencyProperty.Register( + nameof(Inverter), + typeof(bool), + typeof(AllIsBoolToVisibilityConverter), + new PropertyMetadata(false) + ); + + /// + /// 反转 + /// + [DefaultValue(false)] + public bool Inverter + { + get => (bool)GetValue(InverterProperty); + set => SetValue(InverterProperty, value); + } +} diff --git a/VPet.Solution/Converters/ConverterBase/CanInverterValueConverter.cs b/VPet.Solution/Converters/ConverterBase/CanInverterValueConverter.cs new file mode 100644 index 0000000..9c7a9a9 --- /dev/null +++ b/VPet.Solution/Converters/ConverterBase/CanInverterValueConverter.cs @@ -0,0 +1,31 @@ +using System.ComponentModel; +using System.Windows; + +namespace HKW.WPF.Converters; + +/// +/// 可反转值转换器 +/// +public abstract class CanInverterValueConverter : ValueConverterBase + where T : CanInverterValueConverter +{ + /// + /// + /// + public static readonly DependencyProperty InverterProperty = DependencyProperty.Register( + nameof(Inverter), + typeof(bool), + typeof(AllIsBoolToVisibilityConverter), + new PropertyMetadata(false) + ); + + /// + /// 反转 + /// + [DefaultValue(false)] + public bool Inverter + { + get => (bool)GetValue(InverterProperty); + set => SetValue(InverterProperty, value); + } +} diff --git a/VPet.Solution/Converters/ConverterBase/ConverterBase.cs b/VPet.Solution/Converters/ConverterBase/ConverterBase.cs new file mode 100644 index 0000000..875fc41 --- /dev/null +++ b/VPet.Solution/Converters/ConverterBase/ConverterBase.cs @@ -0,0 +1,15 @@ +using System.Globalization; +using System.Windows; + +namespace HKW.WPF.Converters; + +/// +/// 值转换器基础 +/// +public abstract class ConverterBase : DependencyObject +{ + /// + /// 未设置值 + /// + public static readonly object UnsetValue = DependencyProperty.UnsetValue; +} diff --git a/VPet.Solution/Converters/ConverterBase/HaveRatioConverter.cs b/VPet.Solution/Converters/ConverterBase/HaveRatioConverter.cs new file mode 100644 index 0000000..bb3ff52 --- /dev/null +++ b/VPet.Solution/Converters/ConverterBase/HaveRatioConverter.cs @@ -0,0 +1,32 @@ +using System.ComponentModel; +using System.Windows; + +namespace HKW.WPF.Converters; + +/// +/// 含有比率的转换器 +/// +/// 转换器类型 +public abstract class HaveRatioConverter : MultiValueConverterBase + where T : HaveRatioConverter +{ + /// + /// + /// + public static readonly DependencyProperty HaveRatioProperty = DependencyProperty.Register( + nameof(HaveRatio), + typeof(bool), + typeof(T), + new PropertyMetadata(false) + ); + + /// + /// 含有比例 + /// + [DefaultValue(false)] + public bool HaveRatio + { + get => (bool)GetValue(HaveRatioProperty); + set => SetValue(HaveRatioProperty, value); + } +} diff --git a/VPet.Solution/Converters/ConverterBase/MultiValueConverterBase.cs b/VPet.Solution/Converters/ConverterBase/MultiValueConverterBase.cs new file mode 100644 index 0000000..c2700c4 --- /dev/null +++ b/VPet.Solution/Converters/ConverterBase/MultiValueConverterBase.cs @@ -0,0 +1,45 @@ +using System.Globalization; +using System.Windows.Data; + +namespace HKW.WPF.Converters; + +/// +/// 多个值转换器 +/// +public abstract class MultiValueConverterBase : ConverterBase, IMultiValueConverter +{ + /// + /// 转换 + /// + /// 值 + /// 目标类型 + /// 参数 + /// 文化 + /// 转换后的值 + public abstract object Convert( + object[] values, + Type targetType, + object parameter, + CultureInfo culture + ); + + /// + /// 转换回调 + /// + /// 值 + /// 目标类型 + /// 参数 + /// 文化 + /// 转换后的值 + public virtual object[] ConvertBack( + object value, + Type[] targetType, + object parameter, + CultureInfo culture + ) + { + throw new NotSupportedException( + "Converter '" + this.GetType().Name + "' does not support backward conversion." + ); + } +} diff --git a/VPet.Solution/Converters/ConverterBase/MultiValueToBoolConverter.cs b/VPet.Solution/Converters/ConverterBase/MultiValueToBoolConverter.cs new file mode 100644 index 0000000..abd981c --- /dev/null +++ b/VPet.Solution/Converters/ConverterBase/MultiValueToBoolConverter.cs @@ -0,0 +1,31 @@ +using System.ComponentModel; +using System.Windows; + +namespace HKW.WPF.Converters; + +/// +/// 转换至布尔转换器基础 +/// +public abstract class MultiValueToBoolConverter : MultiValueConverterBase + where T : MultiValueToBoolConverter +{ + /// + /// + /// + public static readonly DependencyProperty TargetBoolValueProperty = DependencyProperty.Register( + nameof(TargetBoolValue), + typeof(bool), + typeof(T), + new PropertyMetadata(false) + ); + + /// + /// 指定值 + /// + [DefaultValue(false)] + public bool TargetBoolValue + { + get => (bool)GetValue(TargetBoolValueProperty); + set => SetValue(TargetBoolValueProperty, value); + } +} diff --git a/VPet.Solution/Converters/ConverterBase/ValueConverterBase.cs b/VPet.Solution/Converters/ConverterBase/ValueConverterBase.cs new file mode 100644 index 0000000..f3b56ff --- /dev/null +++ b/VPet.Solution/Converters/ConverterBase/ValueConverterBase.cs @@ -0,0 +1,45 @@ +using System.Globalization; +using System.Windows.Data; + +namespace HKW.WPF.Converters; + +/// +/// 值转换器 +/// +public abstract class ValueConverterBase : ConverterBase, IValueConverter +{ + /// + /// 转换 + /// + /// 值 + /// 目标类型 + /// 参数 + /// 文化 + /// 转换后的值 + public abstract object Convert( + object value, + Type targetType, + object parameter, + CultureInfo culture + ); + + /// + /// 转换回调 + /// + /// 值 + /// 目标类型 + /// 参数 + /// 文化 + /// 转换后的值 + public virtual object ConvertBack( + object value, + Type targetType, + object parameter, + CultureInfo culture + ) + { + throw new NotSupportedException( + "Converter '" + GetType().Name + "' does not support backward conversion." + ); + } +} diff --git a/VPet.Solution/Converters/EqualsConverter.cs b/VPet.Solution/Converters/EqualsConverter.cs index 84c7a92..62a8b20 100644 --- a/VPet.Solution/Converters/EqualsConverter.cs +++ b/VPet.Solution/Converters/EqualsConverter.cs @@ -1,28 +1,37 @@ -using System.Windows.Data; +using System.Globalization; -namespace VPet.House.Converters; +namespace HKW.WPF.Converters; -public class EqualsConverter : IMultiValueConverter +/// +/// 相等转换器 +/// 示例: +/// +/// +/// +/// +/// ]]> +/// +public class EqualsConverter : CanInverterMultiValueConverter { - public object Convert( + /// + /// + /// + /// + /// + /// + /// + /// + /// + public override object Convert( object[] values, Type targetType, object parameter, - System.Globalization.CultureInfo culture + CultureInfo culture ) { if (values.Length != 2) throw new NotImplementedException("Values length must be 2"); - return values[0].Equals(values[1]); - } - - public object[] ConvertBack( - object value, - Type[] targetTypes, - object parameter, - System.Globalization.CultureInfo culture - ) - { - throw new NotImplementedException(); + return values[0].Equals(values[1]) ^ Inverter; } } diff --git a/VPet.Solution/Converters/FalseToCollapsedConverter.cs b/VPet.Solution/Converters/FalseToCollapsedConverter.cs deleted file mode 100644 index 9fbe94b..0000000 --- a/VPet.Solution/Converters/FalseToCollapsedConverter.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Globalization; -using System.Windows; -using System.Windows.Data; - -namespace VPet.House.Converters; - -public class FalseToCollapsedConverter : IValueConverter -{ - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) - { - return (bool.TryParse(value.ToString(), out var result) && result) - ? Visibility.Visible - : Visibility.Collapsed; - } - - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - return value is Visibility visibility && visibility == Visibility.Collapsed; - } -} diff --git a/VPet.Solution/Converters/FalseToHiddenConverter.cs b/VPet.Solution/Converters/FalseToHiddenConverter.cs deleted file mode 100644 index b0856df..0000000 --- a/VPet.Solution/Converters/FalseToHiddenConverter.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Globalization; -using System.Windows; -using System.Windows.Data; - -namespace VPet.House.Converters; - -public class FalseToHiddenConverter : IValueConverter -{ - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) - { - return (bool.TryParse(value.ToString(), out var result) && result) - ? Visibility.Visible - : Visibility.Hidden; - } - - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - return value is Visibility visibility && visibility == Visibility.Hidden; - } -} diff --git a/VPet.Solution/Converters/IsBoolConverter.cs b/VPet.Solution/Converters/IsBoolConverter.cs new file mode 100644 index 0000000..cff414f --- /dev/null +++ b/VPet.Solution/Converters/IsBoolConverter.cs @@ -0,0 +1,62 @@ +using System.ComponentModel; +using System.Globalization; +using System.Windows; + +namespace HKW.WPF.Converters; + +/// +/// 是布尔值转换器 +/// +public class IsBoolConverter : ValueConverterBase +{ + /// + /// + /// + public static readonly DependencyProperty BoolValueProperty = DependencyProperty.Register( + nameof(BoolValue), + typeof(bool), + typeof(AllIsBoolToVisibilityConverter), + new PropertyMetadata(true) + ); + + /// + /// 目标布尔值 + /// + [DefaultValue(true)] + public bool BoolValue + { + get => (bool)GetValue(BoolValueProperty); + set => SetValue(BoolValueProperty, value); + } + + /// + /// + /// + public static readonly DependencyProperty NullValueProperty = DependencyProperty.Register( + nameof(NullValue), + typeof(bool), + typeof(AllIsBoolToVisibilityConverter), + new PropertyMetadata(false) + ); + + /// + /// 为空值时布尔值 + /// + [DefaultValue(false)] + public bool NullValue + { + get => (bool)GetValue(NullValueProperty); + set => SetValue(NullValueProperty, value); + } + + /// + public override object Convert( + object value, + Type targetType, + object parameter, + CultureInfo culture + ) + { + return Utils.GetBool(value, BoolValue, NullValue); + } +} diff --git a/VPet.Solution/Converters/MarginConverter.cs b/VPet.Solution/Converters/MarginConverter.cs index 2575c79..83feab4 100644 --- a/VPet.Solution/Converters/MarginConverter.cs +++ b/VPet.Solution/Converters/MarginConverter.cs @@ -1,7 +1,7 @@ -using System.Windows; -using System.Windows.Data; +using System.Globalization; +using System.Windows; -namespace VPet.House.Converters; +namespace HKW.WPF.Converters; /// /// 边距转换器 @@ -15,70 +15,124 @@ namespace VPet.House.Converters; /// /// ]]> /// -public class MarginConverter : IMultiValueConverter +public class MarginConverter : HaveRatioConverter { - public object Convert( + /// + /// + /// + /// + /// + /// + /// + /// + /// + public override object Convert( object[] values, Type targetType, object parameter, - System.Globalization.CultureInfo culture + CultureInfo culture ) { + if (values.Any(i => i == DependencyProperty.UnsetValue)) + return new Thickness(); if (values.Length == 0) { return new Thickness(); } - else if (values.Length == 1) + if (HaveRatio) { - return new Thickness() + if (values.Length == 1) { - Left = System.Convert.ToDouble(values[0]), - Top = default, - Right = default, - Bottom = default - }; - } - else if (values.Length == 2) - { - return new Thickness() + return new Thickness(); + } + var ratio = System.Convert.ToDouble(values[0]); + if (values.Length == 2) { - Left = System.Convert.ToDouble(values[0]), - Top = System.Convert.ToDouble(values[1]), - Right = default, - Bottom = default - }; - } - else if (values.Length == 3) - { - return new Thickness() + return new Thickness() + { + Left = System.Convert.ToDouble(values[1]) * ratio, + Top = default, + Right = default, + Bottom = default + }; + } + else if (values.Length == 3) { - Left = System.Convert.ToDouble(values[0]), - Top = System.Convert.ToDouble(values[1]), - Right = System.Convert.ToDouble(values[2]), - Bottom = default - }; - } - else if (values.Length == 4) - { - return new Thickness() + return new Thickness() + { + Left = System.Convert.ToDouble(values[1]) * ratio, + Top = System.Convert.ToDouble(values[2]) * ratio, + Right = default, + Bottom = default + }; + } + else if (values.Length == 4) { - Left = System.Convert.ToDouble(values[0]), - Top = System.Convert.ToDouble(values[1]), - Right = System.Convert.ToDouble(values[2]), - Bottom = System.Convert.ToDouble(values[3]) - }; + return new Thickness() + { + Left = System.Convert.ToDouble(values[1]) * ratio, + Top = System.Convert.ToDouble(values[2]) * ratio, + Right = System.Convert.ToDouble(values[3]) * ratio, + Bottom = default + }; + } + else if (values.Length == 5) + { + return new Thickness() + { + Left = System.Convert.ToDouble(values[1]) * ratio, + Top = System.Convert.ToDouble(values[2]) * ratio, + Right = System.Convert.ToDouble(values[3]) * ratio, + Bottom = System.Convert.ToDouble(values[4]) * ratio + }; + } + else + throw new NotImplementedException(); } else - throw new NotImplementedException(); - } - - public object[] ConvertBack( - object value, - Type[] targetTypes, - object parameter, - System.Globalization.CultureInfo culture - ) - { - throw new NotImplementedException(); + { + if (values.Length == 1) + { + return new Thickness() + { + Left = System.Convert.ToDouble(values[0]), + Top = default, + Right = default, + Bottom = default + }; + } + else if (values.Length == 2) + { + return new Thickness() + { + Left = System.Convert.ToDouble(values[0]), + Top = System.Convert.ToDouble(values[1]), + Right = default, + Bottom = default + }; + } + else if (values.Length == 3) + { + return new Thickness() + { + Left = System.Convert.ToDouble(values[0]), + Top = System.Convert.ToDouble(values[1]), + Right = System.Convert.ToDouble(values[2]), + Bottom = default + }; + } + else if (values.Length == 4) + { + return new Thickness() + { + Left = System.Convert.ToDouble(values[0]), + Top = System.Convert.ToDouble(values[1]), + Right = System.Convert.ToDouble(values[2]), + Bottom = System.Convert.ToDouble(values[3]) + }; + } + else + throw new NotImplementedException(); + } } } diff --git a/VPet.Solution/Converters/MaxConverter.cs b/VPet.Solution/Converters/MaxConverter.cs index 3880126..a91d8fb 100644 --- a/VPet.Solution/Converters/MaxConverter.cs +++ b/VPet.Solution/Converters/MaxConverter.cs @@ -1,10 +1,12 @@ using System.Windows.Data; -namespace VPet.House.Converters; +using System.Windows; -public class MaxConverter : IMultiValueConverter +namespace HKW.WPF.Converters; + +public class MaxConverter : MultiValueConverterBase { - public object Convert( + public override object Convert( object[] values, Type targetType, object parameter, @@ -13,14 +15,4 @@ public class MaxConverter : IMultiValueConverter { return values.Max(i => (double)i); } - - public object[] ConvertBack( - object value, - Type[] targetTypes, - object parameter, - System.Globalization.CultureInfo culture - ) - { - throw new NotImplementedException(); - } } diff --git a/VPet.Solution/Converters/MediaColorToBrushConverter.cs b/VPet.Solution/Converters/MediaColorToBrushConverter.cs index e513ca1..8d11f30 100644 --- a/VPet.Solution/Converters/MediaColorToBrushConverter.cs +++ b/VPet.Solution/Converters/MediaColorToBrushConverter.cs @@ -1,22 +1,55 @@ using System.Globalization; -using System.Windows.Data; using System.Windows.Media; -namespace VPet.House.Converters; +namespace HKW.WPF.Converters; -public class MediaColorToBrushConverter : IValueConverter +/// +/// 媒体颜色至画笔颜色转换器 +/// +public class MediaColorToBrushConverter : ValueConverterBase { - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + /// + /// + /// + /// + /// + /// + /// + /// + /// + public override object Convert( + object value, + Type targetType, + object parameter, + CultureInfo culture + ) { if (value is not Color color) - throw new ArgumentException("Not media color", nameof(value)); + throw new ArgumentException($"Not type: {typeof(Color).FullName}", nameof(value)); return new SolidColorBrush(color); } - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + /// + /// + /// + /// + /// + /// + /// + /// + /// + public override object ConvertBack( + object value, + Type targetType, + object parameter, + CultureInfo culture + ) { if (value is not SolidColorBrush brush) - throw new ArgumentException("Not SolidColorBrush", nameof(value)); + throw new ArgumentException( + $"Not type: {typeof(SolidColorBrush).FullName}", + nameof(value) + ); return brush.Color; } } diff --git a/VPet.Solution/Converters/NotEqualsConverter.cs b/VPet.Solution/Converters/NotEqualsConverter.cs deleted file mode 100644 index 0559d8f..0000000 --- a/VPet.Solution/Converters/NotEqualsConverter.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System.Windows.Data; - -namespace VPet.House.Converters; - -public class NotEqualsConverter : IMultiValueConverter -{ - public object Convert( - object[] values, - Type targetType, - object parameter, - System.Globalization.CultureInfo culture - ) - { - if (values.Length != 2) - throw new NotImplementedException("Values length must be 2"); - return !values[0].Equals(values[1]); - } - - public object[] ConvertBack( - object value, - Type[] targetTypes, - object parameter, - System.Globalization.CultureInfo culture - ) - { - throw new NotImplementedException(); - } -} diff --git a/VPet.Solution/Converters/NullToFalseConverter.cs b/VPet.Solution/Converters/NullToFalseConverter.cs deleted file mode 100644 index 909721c..0000000 --- a/VPet.Solution/Converters/NullToFalseConverter.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Globalization; -using System.Windows.Data; - -namespace VPet.House.Converters; - -public class NullToFalseConverter : IValueConverter -{ - public object Convert(object value, Type targetType, object parameter, CultureInfo culture) - { - return value is not null; - } - - public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - throw new NotImplementedException(); - } -} diff --git a/VPet.Solution/Converters/RatioMarginConverter.cs b/VPet.Solution/Converters/RatioMarginConverter.cs deleted file mode 100644 index 5e31dac..0000000 --- a/VPet.Solution/Converters/RatioMarginConverter.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System.Windows; -using System.Windows.Data; - -namespace VPet.House.Converters; - -/// -/// 边距转换器 -/// 示例: -/// -/// -/// -/// -/// -/// -/// ]]> -/// -public class RatioMarginConverter : IMultiValueConverter -{ - public object Convert( - object[] values, - Type targetType, - object parameter, - System.Globalization.CultureInfo culture - ) - { - if (values.Any(i => i == DependencyProperty.UnsetValue)) - return new Thickness(); - if (values.Length == 0) - { - return new Thickness(); - } - else if (values.Length == 1) - { - return new Thickness(); - } - var ratio = System.Convert.ToDouble(values[0]); - if (values.Length == 2) - { - return new Thickness() - { - Left = System.Convert.ToDouble(values[1]) * ratio, - Top = default, - Right = default, - Bottom = default - }; - } - else if (values.Length == 3) - { - return new Thickness() - { - Left = System.Convert.ToDouble(values[1]) * ratio, - Top = System.Convert.ToDouble(values[2]) * ratio, - Right = default, - Bottom = default - }; - } - else if (values.Length == 4) - { - return new Thickness() - { - Left = System.Convert.ToDouble(values[1]) * ratio, - Top = System.Convert.ToDouble(values[2]) * ratio, - Right = System.Convert.ToDouble(values[3]) * ratio, - Bottom = default - }; - } - else if (values.Length == 5) - { - return new Thickness() - { - Left = System.Convert.ToDouble(values[1]) * ratio, - Top = System.Convert.ToDouble(values[2]) * ratio, - Right = System.Convert.ToDouble(values[3]) * ratio, - Bottom = System.Convert.ToDouble(values[4]) * ratio - }; - } - else - throw new NotImplementedException(); - } - - public object[] ConvertBack( - object value, - Type[] targetTypes, - object parameter, - System.Globalization.CultureInfo culture - ) - { - throw new NotImplementedException(); - } -} diff --git a/VPet.Solution/Converters/StringFormatConverter.cs b/VPet.Solution/Converters/StringFormatConverter.cs index 26730e0..67eff9c 100644 --- a/VPet.Solution/Converters/StringFormatConverter.cs +++ b/VPet.Solution/Converters/StringFormatConverter.cs @@ -1,9 +1,9 @@ -using System.Windows.Data; +using System.Globalization; -namespace VPet.House.Converters; +namespace HKW.WPF.Converters; /// -/// 边距转换器 +/// 字符串格式化器 /// 示例: /// @@ -18,13 +18,21 @@ namespace VPet.House.Converters; /// /// ]]> /// -public class StringFormatConverter : IMultiValueConverter +public class StringFormatConverter : MultiValueConverterBase { - public object Convert( + /// + /// + /// + /// + /// + /// + /// + /// + public override object Convert( object[] values, Type targetType, object parameter, - System.Globalization.CultureInfo culture + CultureInfo culture ) { var formatStr = (string)parameter; @@ -38,14 +46,4 @@ public class StringFormatConverter : IMultiValueConverter return string.Format(formatStr, values); } } - - public object[] ConvertBack( - object value, - Type[] targetTypes, - object parameter, - System.Globalization.CultureInfo culture - ) - { - throw new NotImplementedException(); - } } diff --git a/VPet.Solution/Converters/ValueToBoolConverter.cs b/VPet.Solution/Converters/ValueToBoolConverter.cs new file mode 100644 index 0000000..a44bad2 --- /dev/null +++ b/VPet.Solution/Converters/ValueToBoolConverter.cs @@ -0,0 +1,22 @@ +using HKW.WPF.Converters; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HKW.WPF.Converters; + +public class ValueToBoolConverter : ValueConverterBase +{ + public override object Convert( + object value, + Type targetType, + object parameter, + CultureInfo culture + ) + { + throw new NotImplementedException(); + } +} diff --git a/VPet.Solution/Utils/Utils.cs b/VPet.Solution/Utils/Utils.cs index c628c1b..367f70c 100644 --- a/VPet.Solution/Utils/Utils.cs +++ b/VPet.Solution/Utils/Utils.cs @@ -76,4 +76,16 @@ public static class Utils } return bitmapImage; } + + public static bool GetBool(object value, bool boolValue, bool nullValue) + { + if (value is null) + return nullValue; + else if (value is bool b) + return b == boolValue; + else if (bool.TryParse(value.ToString(), out b)) + return b == boolValue; + else + return false; + } } diff --git a/VPet.Solution/VPet.Solution.csproj b/VPet.Solution/VPet.Solution.csproj index 10a4148..3ff2d7a 100644 --- a/VPet.Solution/VPet.Solution.csproj +++ b/VPet.Solution/VPet.Solution.csproj @@ -74,6 +74,26 @@ MSBuild:Compile Designer + + + + + + + + + + + + + + + + + + + + @@ -146,18 +166,6 @@ MainWindow.xaml - - - - - - - - - - - -