Files
WinWARP/WinWARP.Ui/Converters/BoolToVisibilityConverter.cs
T
2026-09-22 15:23:39 +03:00

53 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Markup;
namespace WinWARP.Ui.Converters;
[MarkupExtensionReturnType(ReturnType = typeof(IValueConverter))]
public partial class BoolToVisibilityConverter : MarkupExtension, IValueConverter
{
public bool IsInverted { get; set; } = false;
// IValueConverter implementation
public object Convert(object value, Type targetType, object parameter, string language)
{
bool flag = false;
if (value is bool boolValue)
{
flag = boolValue;
}
var parameterInverted = false;
if (parameter is string paramStr && bool.TryParse(paramStr, out var result))
{
parameterInverted = result;
}
if (IsInverted || parameterInverted)
{
flag = !flag;
}
return flag ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
if (value is Visibility visibility)
{
bool result = (visibility == Visibility.Visible);
return IsInverted ? !result : result;
}
return false;
}
// MarkupExtension override returning self as the instance
protected override object ProvideValue()
{
return this;
}
}