添加项目文件。
This commit is contained in:
40
UIShare/Converters/BooleanToVisibilityConverter.cs
Normal file
40
UIShare/Converters/BooleanToVisibilityConverter.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace UIShare.Converters
|
||||
{
|
||||
public class BooleanToVisibilityConverter : IValueConverter
|
||||
{
|
||||
// 同时接受 "invert" / "inverse" / "inverted" / "reverse"(大小写不敏感)作为反转参数,
|
||||
// 避免因 XAML 处用了 ConverterParameter=Inverse 而静默失效。
|
||||
private static bool IsInvert(object parameter)
|
||||
{
|
||||
var s = parameter?.ToString()?.ToLowerInvariant();
|
||||
return s == "invert" || s == "inverse" || s == "inverted" || s == "reverse";
|
||||
}
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
bool input = value is bool b && b;
|
||||
if (IsInvert(parameter))
|
||||
input = !input;
|
||||
|
||||
return input ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
bool output = value is Visibility v && v == Visibility.Visible;
|
||||
if (IsInvert(parameter))
|
||||
output = !output;
|
||||
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user