ref: 迁移 ValueConverter

This commit is contained in:
龙腾猫跃 committed 2026-05-07 11:26:31 +08:00
1 parent 2d682afd2c
commit 9a77157dcf
3 files changed
+76 -1

No files matched your search

+1 -1
View File
@@ -56,7 +56,7 @@ public enum LogBehavior {
} }
public static class Logger { public static class Logger {
internal static BaseLogger Instance { get; set; } = new(); public static BaseLogger Instance { get; set; } = new();
// 转发给实例的方法包装 // 转发给实例的方法包装
public static void Log(string message, LogLevel level = LogLevel.Info, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "") public static void Log(string message, LogLevel level = LogLevel.Info, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "")
+74
View File
@@ -0,0 +1,74 @@
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace MeloongCore.Wpf;
/// <summary>
/// 对数据绑定进行加法运算,使用 <paramref name="parameter"/> 决定加数。
/// </summary>
public class AdditionConverter : IValueConverter {
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
if (value is null) return 0;
if (!double.TryParse(value.ToString(), out double before)) return 0;
double scale = 1;
if (parameter is not null) double.TryParse(parameter.ToString(), out scale);
return before + scale;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
if (value is null) return Binding.DoNothing;
if (!double.TryParse(value.ToString(), out double before)) return Binding.DoNothing;
double scale = 1;
if (parameter is not null) double.TryParse(parameter.ToString(), out scale);
return before - scale;
}
}
/// <summary>
/// 对数据绑定进行乘法运算,使用 <paramref name="parameter"/> 决定乘数。
/// </summary>
public class MultiplicationConverter : IValueConverter {
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
if (value is null) return 0;
if (!double.TryParse(value.ToString(), out double before)) return 0;
double scale = 1;
if (parameter is not null) double.TryParse(parameter.ToString(), out scale);
return before * scale;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
if (value is null) return Binding.DoNothing;
if (!double.TryParse(value.ToString(), out double before)) return Binding.DoNothing;
double scale = 1;
if (parameter is not null) double.TryParse(parameter.ToString(), out scale);
if (scale == 0) return Binding.DoNothing;
return before / scale;
}
}
/// <summary>
/// 将取反的 <see cref="bool"/> 绑定到 <see cref="Visibility"/>。
/// </summary>
public class InverseBooleanToVisibilityConverter : IValueConverter {
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
if (value is null) return Visibility.Visible;
return bool.TryParse(value.ToString(), out bool boolValue) ? (boolValue ? Visibility.Collapsed : Visibility.Visible) : Visibility.Visible;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
if (value is null) return false;
return value is Visibility vis && vis != Visibility.Visible;
}
}
/// <summary>
/// 将 <see cref="bool"/> 取反。
/// </summary>
public class InverseBooleanConverter : IValueConverter {
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) {
if (value is null) return false;
return bool.TryParse(value.ToString(), out bool boolValue) ? !boolValue : false;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) {
if (value is null) return false;
return bool.TryParse(value.ToString(), out bool boolValue) ? !boolValue : false;
}
}
+1
View File
@@ -38,6 +38,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" /> <Reference Include="PresentationFramework" />
</ItemGroup> </ItemGroup>