当前位置: 首页 > news >正文

WPF中Converter基础用法

IValueConverter

1.创建一个类集成接口IValueConverter,并实现

2在xaml中引入

举例

image-20250626172015686

性别用int来表示,1为男2为女

核心代码

创建GenderConverter继承IValueConverter

public class GenderConverter : IValueConverter
{//model->view转换public object Convert(object value, Type targetType, object parameter, CultureInfo culture){if (value == null || parameter == null)return false;return value.ToString() == parameter.ToString();//throw new NotImplementedException();}//view->model转换public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){//throw new NotImplementedException();return parameter;}}

ViewModel中定义属性

public int Gender { get; set; }

xaml中引入

xmlns:convert="clr-namespace:TestConvert.Converter"
<Window.Resources><convert:GenderConverter x:Key="genderConverter" />
</Window.Resources>

xaml中IsChecked绑定Gender

<StackPanel Grid.Row="1" Orientation="Horizontal"><TextBlock Margin="10,0" Text="性别:" /><RadioButton Margin="0,0,10,0" Content="男" IsChecked="{Binding Gender, Converter={StaticResource genderConverter}, ConverterParameter=1}" /><RadioButton Content="女" IsChecked="{Binding Gender, Converter={StaticResource genderConverter}, ConverterParameter=2}" />
</StackPanel>

IMultiValueConverter

1.创建一个类集成接口IMultiValueConverter,并实现

2在xaml中引入

举例

image-20250626172732803

image-20250626172749906

只有当下拉框一个为A一个为B才会显示绿色,其他都为红色

核心代码

通过多帮获取两个对象的数据,在ComboBoxConverter中进行验证

public class ComboBoxConverter : IMultiValueConverter
{public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture){if (values[0].ToString() == "A"&& values[1].ToString() == "B"){return Brushes.Green;}else{return Brushes.Red;}}public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture){throw new NotImplementedException();}
}

xaml中引入Converter资源

xmlns:convert="clr-namespace:TestConvert.Converter"
<Window.Resources><convert:ComboBoxConverter x:Key="comboBoxConverter" 
</Window.Resources>

xaml中引入ComboBoxConverter

<StackPanel Grid.Row="2" Orientation="Horizontal"><ComboBoxName="cb_001"Width="60"Height="25"Margin="0,30,0,0"HorizontalAlignment="Center"VerticalAlignment="Center"><ComboBoxItem Content="A" /><ComboBoxItem Content="B" /><ComboBoxItem Content="C" /></ComboBox><ComboBoxName="cb_002"Width="60"Height="25"Margin="0,10,0,0"HorizontalAlignment="Center"VerticalAlignment="Center"><ComboBoxItem Content="A" /><ComboBoxItem Content="B" /><ComboBoxItem Content="C" /></ComboBox><TextBlock Margin="0,20,0,0" HorizontalAlignment="Center" Text="Hello World!"><TextBlock.Foreground><MultiBinding Converter="{StaticResource comboBoxConverter}"><Binding Path="Text" ElementName="cb_001" /><Binding Path="Text" ElementName="cb_002" /></MultiBinding></TextBlock.Foreground></TextBlock>
</StackPanel>
http://www.dtcms.com/a/259732.html

相关文章:

  • 众趣科技与中原地产达成VR看房技术合作,赋能房产经纪数字化转型
  • C++ 快速回顾(二)
  • heygem报错Error: Error invoking remote method ‘model/addModel‘:
  • 16.1 Python应用容器化终极指南:Dockerfile多阶段构建与安全优化实战
  • 适合初学者(kubernetes)k8s脚本安装
  • .NET测试工具Parasoft dotTEST内置安全标准,编码合规更高效
  • 基于 SpringBoot+JSP+ElementUI+jQuery 助农生鲜销售系统设计与实现
  • 使用ASIO的协程实现高并发服务器
  • 数据结构-第三节-树与二叉树
  • 《汇编语言:基于X86处理器》第5章 过程(1)
  • IDE如何快速切换JLINK版本
  • HarmonyOs开发之——TypeScript介绍、入门,及 TypeScript、JavaScript、ArkTs的具体区别解读。
  • 制药行业的精细化管理:GCOM80-2NET自动化解决方案
  • Python+selenium自动化生成测试报告
  • 营业额统计-02.代码开发及功能测试
  • 命名数据网络 | 兴趣包(Interest Packet)
  • GitLab 18.1 正式发布Maven 虚拟仓库、密码泄露检测等功能,可升级体验!
  • 广州华锐互动:技术与创意双驱动的 VR 先锋​
  • Claude 3.7 的 token 预算机制详解:可控深度的混合推理范式
  • HDFS(Hadoop分布式文件系统)总结
  • 【缓存技术】深入分析如果使用好缓存及注意事项
  • 基于SpringBoot和Leaflet的区域冲突可视化-以伊以冲突为例
  • 6.26_JAVA_微服务_Elasticsearch
  • CRON表达式编辑器与定时任务实现技术文档
  • Linux 统一方式安装多版本 JDK 指南
  • LINUX 626 DNS报错
  • 【工具推荐】WaybackLister——发现潜在目录列表
  • JavaEE:分布式session
  • 2025学年湖北省职业院校技能大赛 “信息安全管理与评估”赛项 样题卷(五)
  • centos 7 安装NVIDIA Container Toolkit