自定义Converter
一般转换器
xmlns:cvt="clr-namespace:Project1.Common.Converter"
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="boolToVisibilityConverter"/>
<cvt:IsNullOrEmptyConverter x:Key="isNullOrEmptyConverter " />
</UserControl.Resources>
Half
IsNullOrEmpty
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
internal class IsNullOrEmptyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.IsNullOrEmpty((string)value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
Number2PercentageConverter
public class Number2PercentageConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || values.Length != 2) return .0;
var obj1 = values[0];
var obj2 = values[1];
if (obj1 == null || obj2 == null) return .0;
var str1 = values[0].ToString();
var str2 = values[1].ToString();
var v1 = str1.Value<double>();
var v2 = str2.Value<double>();
if (MathHelper.IsVerySmall(v2)) return 100.0;
return v1 / v2 * 100;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
String2VisibilityConverter
public class String2VisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.IsNullOrEmpty((string)value) ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null && (Visibility)value == Visibility.Collapsed;
}
}
ImagePathConverter
public class ImagePathConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string path = (string)value;
if (!string.IsNullOrEmpty(path))
{
return new System.Windows.Media.Imaging.BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
}
else
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
IndentConverter
public class IndentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double colunwidth = 10;
double left = 0.0;
UIElement element = value as TreeViewItem;
while (element.GetType() != typeof(TreeView))
{
element = (UIElement)VisualTreeHelper.GetParent(element);
if (element.GetType() == typeof(TreeViewItem))
left += colunwidth;
}
return new Thickness(left, 0, 0, 0);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
TransparentColorConverter
Color="{Binding SolidColorBrushKey1, Converter={StaticResource transparentColorConverter}, ConverterParameter=0.12}"
/// <summary>
/// 设置颜色透明度
/// </summary>
public class TransparentColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is SolidColorBrush brush && parameter is string txt)
{
var alpha = txt.Value<double>();
var a = alpha * 255;
return Color.FromArgb((byte)a, brush.Color.R, brush.Color.G, brush.Color.B);
}
return DependencyProperty.UnsetValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}