HKW.CommonValueConverters
0.1.2
dotnet add package HKW.CommonValueConverters --version 0.1.2
NuGet\Install-Package HKW.CommonValueConverters -Version 0.1.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HKW.CommonValueConverters" Version="0.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HKW.CommonValueConverters --version 0.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HKW.CommonValueConverters, 0.1.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HKW.CommonValueConverters as a Cake Addin #addin nuget:?package=HKW.CommonValueConverters&version=0.1.2 // Install HKW.CommonValueConverters as a Cake Tool #tool nuget:?package=HKW.CommonValueConverters&version=0.1.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HKW.CommonValueConverters
A common value or multi value converter base
Use in WPF
ConverterBase
ConverterBase
and CommonDependencyProperty
public class CommonDependencyProperty
{
public static CommonDependencyProperty<TProperty> Register<TOwner, TProperty>(
string propertyName
)
{
var dependencyProperty = DependencyProperty.Register(
propertyName,
typeof(TProperty),
typeof(TOwner)
);
return new(dependencyProperty);
}
public static CommonDependencyProperty<TProperty> Register<TOwner, TProperty>(
string propertyName,
TProperty defaultValue
)
{
var dependencyProperty = DependencyProperty.Register(
propertyName,
typeof(TProperty),
typeof(TOwner),
new PropertyMetadata(defaultValue)
);
return new(dependencyProperty);
}
}
public abstract class ConverterBase : DependencyObject, ICommonValueConverter
{
protected ConverterBase()
{
CommonConverterBase.UnsetValue = DependencyProperty.UnsetValue;
}
public static readonly object UnsetValue = DependencyProperty.UnsetValue;
public PreferredCulture PreferredCulture { get; set; } =
ValueConvertersConfig.DefaultPreferredCulture;
public T GetValue<T>(CommonDependencyProperty<T> commonDependencyProperty)
{
if (commonDependencyProperty.Value is not DependencyProperty dependency)
throw new ArgumentNullException(nameof(commonDependencyProperty));
return (T)GetValue(dependency);
}
public void SetValue<T>(CommonDependencyProperty<T> commonDependencyProperty, T value)
{
if (commonDependencyProperty.Value is not DependencyProperty dependency)
throw new ArgumentNullException(nameof(commonDependencyProperty));
SetValue(dependency, value);
}
public static readonly CommonDependencyProperty<object> DefaultResultProperty =
CommonDependencyProperty.Register<ConverterBase, object>(nameof(DefaultResult));
public object DefaultResult
{
get => GetValue(DefaultResultProperty);
set => SetValue(DefaultResultProperty, value);
}
public static readonly CommonDependencyProperty<PreferredCulture> PreferredCultureProperty =
CommonDependencyProperty.Register<ConverterBase, PreferredCulture>(
nameof(PreferredCulture)
);
public PreferredCulture PreferredCulture
{
get => GetValue(PreferredCultureProperty);
set => SetValue(PreferredCultureProperty, value);
}
}
ValueConverterBase
public abstract class ValueConverterBase : ConverterBase, IValueConverter
{
private CommonValueConverters.ValueConverterBase? _commonValueConverter;
public CommonValueConverters.ValueConverterBase? CommonValueConverter
{
get => _commonValueConverter;
set => CommonValueConverterInitialize(_commonValueConverter = value!);
}
// Add GetDefaultResult action when setting CommonValueConverter
public virtual void CommonValueConverterInitialize(
CommonValueConverters.ValueConverterBase commonValueConverter
)
{
commonValueConverter.GetDefaultResult = () => DefaultResult;
}
public virtual object? Convert(
object? value,
Type? targetType,
object? parameter,
CultureInfo? culture
)
{
return CommonValueConverter?.Convert(value, targetType, parameter, culture);
}
public virtual object? ConvertBack(
object? value,
Type? targetType,
object? parameter,
CultureInfo? culture
)
{
return CommonValueConverter?.ConvertBack(value, targetType, parameter, culture);
}
object? IValueConverter.Convert(
object value,
Type targetType,
object parameter,
CultureInfo culture
)
{
return Convert(
value,
targetType,
parameter,
ValueConvertersConfig.SelectCulture(PreferredCulture, () => culture)
);
}
object? IValueConverter.ConvertBack(
object value,
Type targetType,
object parameter,
CultureInfo culture
)
{
return ConvertBack(
value,
targetType,
parameter,
ValueConvertersConfig.SelectCulture(PreferredCulture, () => culture)
);
}
}
BoolToValue
public class BoolToValueConverter<T> : InvertibleValueConverterBase
{
/// <inheritdoc/>
public BoolToValueConverter()
{
CommonValueConverter = new CommonValueConverters.BoolToValueConverter<T>()
{
GetTrueValue = () => TrueValue,
GetFalseValue = () => FalseValue,
GetNullValue = () => NullValue,
GetIsNullable = () => IsNullable,
};
}
public static readonly CommonDependencyProperty<T> TrueValueProperty =
CommonDependencyProperty.Register<BoolToValueConverter<T>, T>(nameof(TrueValue));
public T TrueValue
{
get => GetValue(TrueValueProperty);
set => SetValue(TrueValueProperty, value);
}
public static readonly CommonDependencyProperty<T> FalseValueProperty =
CommonDependencyProperty.Register<BoolToValueConverter<T>, T>(nameof(FalseValue));
public T FalseValue
{
get => GetValue(FalseValueProperty);
set => SetValue(FalseValueProperty, value);
}
public static readonly CommonDependencyProperty<bool> IsNullableProperty =
CommonDependencyProperty.Register<BoolToValueConverter<T>, bool>(nameof(IsNullable));
public bool IsNullable
{
get => GetValue(IsNullableProperty);
set => SetValue(IsNullableProperty, value);
}
public static readonly CommonDependencyProperty<T> NullValueProperty =
CommonDependencyProperty.Register<BoolToValueConverter<T>, T>(nameof(NullValue));
public T NullValue
{
get => GetValue(NullValueProperty);
set => SetValue(NullValueProperty, value);
}
}
BoolToVisibilityConverter
public class BoolToVisibilityConverter : BoolToValueConverter<Visibility>
{
public BoolToVisibilityConverter()
{
TrueValue = Visibility.Visible;
FalseValue = Visibility.Collapsed;
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- HKW.Utils (>= 1.3.5)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on HKW.CommonValueConverters:
Package | Downloads |
---|---|
HKW.WPF
A dedicated WPF library providing Collections, TypeExtension, MVVMDialogs, and other features. |
GitHub repositories
This package is not used by any popular GitHub repositories.