WPF 装饰器 、 转换器 、行为
十年河东,十年河西,莫欺少年穷
学无止境,精益求精
行为请参考:WPF 行为
装饰器参考:https://www.cnblogs.com/xietianjiao/p/11239558.html
wpf 转换器
详情参考:https://www.cnblogs.com/zh7791/p/9311332.html
单值转换器需继承自 IValueConverter
public class MyNumberConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if(value!=null&& System.Convert.ToInt32(value) % 2 == 0) { return "偶数"; } return "奇数"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
多值转换器需继承自IMultiValueConverter
public class MyColorConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values != null && values.Length > 2) { var R = System.Convert.ToByte(values[0]); var G = System.Convert.ToByte(values[1]); var B = System.Convert.ToByte(values[2]); System.Windows.Media.Color color = System.Windows.Media.Color.FromRgb(R,G,B); return new SolidColorBrush(color); } return null; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
使用转换器
xaml 如下:

<Window x:Class="WpfApp9.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:MyConverter="clr-namespace:WpfApp9.Converter" mc:Ignorable="d" Title="MainWindow" Height="450" Width="600"> <Window.Resources> <MyConverter:MyNumberConverter x:Key="MyNumber"/> <MyConverter:MyColorConverter x:Key="MyColor"/> </Window.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0"> <TextBox x:Name="txt" Text="0" Width="100" Height="40" FontFamily="50"/> <TextBlock Text="{Binding ElementName=txt,Path=Text,Converter={StaticResource MyNumber}}" FontSize="50" /> </StackPanel> <StackPanel Grid.Column="1"> <Slider x:Name="Rcolor" Maximum="255" Minimum="0" Value="0" Width="200" Margin="10"/> <Slider x:Name="Gcolor" Maximum="255" Minimum="0" Value="0" Width="200" Margin="10"/> <Slider x:Name="Bcolor" Maximum="255" Minimum="0" Value="0" Width="200" Margin="10"/> <Path VerticalAlignment="Center" Stroke="Black" StrokeThickness="3"> <Path.Data> <EllipseGeometry Center="80,80" RadiusX="60" RadiusY="60"/> </Path.Data> <Path.Fill> <MultiBinding Converter="{StaticResource MyColor}"> <Binding ElementName="Rcolor" Path="Value"></Binding> <Binding ElementName="Gcolor" Path="Value"></Binding> <Binding ElementName="Bcolor" Path="Value"></Binding> </MultiBinding> </Path.Fill> </Path> </StackPanel> </Grid> </Window>
效果:
注:需要在资源中引入定义的转换器
<Window.Resources> <MyConverter:MyNumberConverter x:Key="MyNumber"/> <MyConverter:MyColorConverter x:Key="MyColor"/> </Window.Resources>
使用如下:
<TextBlock Text="{Binding ElementName=txt,Path=Text,Converter={StaticResource MyNumber}}" FontSize="50" />
分类:
WPF+ModBus
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2022-12-20 docker-compose 编排Net6项目,redis/mysql/nlog实战
2019-12-20 git报错 - remote: HTTP Basic: Access denied
2017-12-20 GIT 常用命令
2016-12-20 VS2012 C#使用/配置Log4Net