Converter 类型详解与使用示例
目录
- Normal Converter
- IMultiValueConverter
- FuncValueConverter
为了提高 Converter 的利用率,可以将其实例定义为静态资源,避免重复创建实例。例如:
1
2
3
4
|
/// <summary>
/// 获取 MathAddConverter 的静态实例
/// </summary>
public static MathAddConverter AddConverter { get; } = new MathAddConverter();
|
在 XAML 中调用该静态资源的方式如下:
1
2
|
<NumericUpDown Grid.Row="1" Grid.Column="1"
Value="{Binding Number1, Converter={x:Static MathAddConverter.AddConverter}, ConverterParameter={StaticResource MyConverterParameter}}" />
|
Normal Converter
普通值转换器(IValueConverter):用于单个绑定值的转换,但 ConverterParameter
不支持动态绑定。
示例:AddConverter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public class AddConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is decimal d1 && parameter is decimal d2)
{
return d1 + d2;
}
return null;
}
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is decimal d1 && parameter is decimal d2)
{
return d1 - d2;
}
return null;
}
}
|
XAML 资源定义
1
2
3
4
|
<Window.Resources>
<conv:AddConverter x:Key="AddConverter" />
<x:Decimal x:Key="inputAddValue">2</x:Decimal>
</Window.Resources>
|
使用示例
1
2
3
4
5
6
7
8
9
10
|
<StackPanel Orientation="Horizontal">
<TextBlock Classes="Header" Text="NormalConverter" />
<TextBlock Text="Input a number to sum" />
<NumericUpDown Increment="0.1" Value="{Binding Number}" />
<TextBlock Text="Sum" />
<NumericUpDown
Value="{Binding Number,
Converter={StaticResource AddConverter},
ConverterParameter={StaticResource inputAddValue}}" />
</StackPanel>
|

IMultiValueConverter
多值转换器(IMultiValueConverter):支持多个绑定值输入,ConverterParameter
支持绑定。
示例:MultiValueAddConverter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class MultiValueAddConverter : IMultiValueConverter
{
public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{
if (values.Any(s => s is not decimal))
{
return new BindingNotification(
new InvalidOperationException(
"Not all input parameter type is decimal, this converter only support decimal type"
),
BindingErrorType.Error
);
}
return values.Sum(s => (decimal)s);
}
}
|
使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<StackPanel Orientation="Horizontal">
<TextBlock Classes="Header" Text="MultiValueConverter" />
<TextBlock Text="Input numbers to sum" />
<NumericUpDown Increment="0.1" Value="{Binding Number1}" />
<NumericUpDown Increment="0.1" Value="{Binding Number2}" />
<NumericUpDown Increment="0.1" Value="{Binding Number3}" />
<TextBlock Text="Sum" />
<NumericUpDown IsReadOnly="True">
<NumericUpDown.Value>
<MultiBinding Converter="{StaticResource MultiValueAddConverter}" Mode="OneWay">
<Binding Path="Number1" />
<Binding Path="Number2" />
<Binding Path="Number3" />
</MultiBinding>
</NumericUpDown.Value>
</NumericUpDown>
</StackPanel>
|

FuncValueConverter
函数式转换器(FuncValueConverter):通过委托方式定义转换逻辑,适用于类型明确、逻辑简单的场景。
示例:字符串转画刷
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class FuncValueConverters
{
public static FuncValueConverter<string?, Brush?> StringToBrushFuncConverter { get; } =
new(s =>
{
Color color;
if (Color.TryParse(s, out color) || Color.TryParse($"#{s}", out color))
{
return new SolidColorBrush(color);
}
return null;
});
}
|
使用示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<StackPanel Orientation="Horizontal">
<TextBlock Classes="Header" Text="FuncValueConverter" />
<TextBox
Text="red"
UseFloatingWatermark="True"
Watermark="Type the color to parse (e.g.: red, green, blue, #FF112233)">
<TextBox.InnerLeftContent>
<Ellipse
Fill="{Binding $parent[TextBox].Text, Converter={x:Static conv:FuncValueConverters.StringToBrushFuncConverter}}"
Height="20"
Margin="5,0,0,0"
Stroke="Gray"
StrokeThickness="1"
Width="20" />
</TextBox.InnerLeftContent>
</TextBox>
</StackPanel>
|


如需扩展更多转换逻辑,可以继续添加新的 Converter 或使用 FuncValueConverter
快速实现轻量级转换逻辑。