wpf之转换器(Converter)
转换器
作用:可以将源数据和目标数据之间进行特定的转化。
值转换器
将值从一种类型转换成另外一种类型。
比如说实现0与1转换成false和true
后台:
namespace MyWpf { /// <summary> /// MyTestConverter.xaml 的交互逻辑 /// </summary> public partial class MyTestConverter : UserControl { public MyTestConverter() { InitializeComponent(); } } /// <summary> /// 自定义转换器 /// </summary> public class IDisplayConverter : IValueConverter { /// <summary> /// 后台转换往前端传值 /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null) { string str = value.ToString(); if (str == "0") { return false; } return true; } return true; } /// <summary> /// 前端往后端传值 /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }
xaml:
<UserControl x:Class="MyWpf.MyTestConverter" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:MyWpf" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <!--在Usercontrol用户自定义控件中引用资源使用UserControl.Resources标签,如果在Windows中就是要使用<Window.Resources>标签--> <UserControl.Resources> <!--引入转换器资源--> <local:IDisplayConverter x:Key="converter"></local:IDisplayConverter> </UserControl.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <StackPanel> <!--设置默认值,模拟已经从后台取到值了--> <TextBlock x:Name="t1" FontSize="50" Text="0"></TextBlock> <TextBlock x:Name="t2" FontSize="50" Text="1"></TextBlock> <TextBlock x:Name="t3" FontSize="50"
Text="{Binding Path=Text,ElementName=t1,Converter={StaticResource ResourceKey=converter}}"></TextBlock> <TextBlock x:Name="t4" FontSize="50"
Text="{Binding Path=Text,ElementName=t2,Converter={StaticResource ResourceKey=converter}}"></TextBlock> </StackPanel> </Grid> </UserControl>
结果:
多值转换器
后台:
public class IMultiValueDisplayConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values == null || values.Length < 3) return null; byte s1 = System.Convert.ToByte(values[0]); byte s2 = System.Convert.ToByte(values[1]); byte s3 = System.Convert.ToByte(values[2]); // 使用指定的 sRGB 颜色通道值创建一个新的 System.Windows.Media.Color 结构。 Color color = Color.FromRgb(s1,s2,s3); return new SolidColorBrush(color); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
xaml:
<UserControl x:Class="MyWpf.MyTestConverter" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:MyWpf" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> <!--在Usercontrol用户自定义控件中引用资源使用UserControl.Resources标签,如果在Windows中就是要使用<Window.Resources>标签--> <UserControl.Resources> <!--引入转换器资源--> <local:IDisplayConverter x:Key="converter"></local:IDisplayConverter> <local:IMultiValueDisplayConverter x:Key="mul"></local:IMultiValueDisplayConverter> </UserControl.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0"> <!--设置默认值,模拟已经从后台取到值了--> <TextBlock x:Name="t1" FontSize="50" Text="0"></TextBlock> <TextBlock x:Name="t2" FontSize="50" Text="1"></TextBlock> <TextBlock x:Name="t3" FontSize="50" Text="{Binding Path=Text,ElementName=t1,
Converter={StaticResource ResourceKey=converter}}"></TextBlock> <TextBlock x:Name="t4" FontSize="50" Text="{Binding Path=Text,ElementName=t2,
Converter={StaticResource ResourceKey=converter}}"></TextBlock> </StackPanel> <!--多值转换器 --> <StackPanel Grid.Column="1"> <Slider Name="s1" Minimum="0" Maximum="255" Margin="10" Width="200"></Slider> <Slider Name="s2" Minimum="0" Maximum="255" Margin="10" Width="200"></Slider> <Slider Name="s3" Minimum="0" Maximum="255" Margin="10" Width="200"></Slider> <Path HorizontalAlignment="Center" > <Path.Data> <EllipseGeometry Center="100,100" RadiusX="50" RadiusY="50"> </EllipseGeometry> </Path.Data> <Path.Fill> <MultiBinding Converter="{StaticResource ResourceKey=mul}"> <Binding ElementName="s1" Path="Value"></Binding> <Binding ElementName="s2" Path="Value"></Binding> <Binding ElementName="s3" Path="Value"></Binding> </MultiBinding> </Path.Fill> </Path> </StackPanel> </Grid> </UserControl>
拖动3个Slider,圆形颜色也会对应改变
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术