当前位置: 首页 > 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.lryc.cn/news/575458.html

相关文章:

  • OceanBase SQL 引擎高级技术学习笔记(通俗篇)
  • 智能制造——58页智慧工厂解决方案【附全文阅读】
  • python中学物理实验模拟:斜面受力分析
  • Elasticsearch 中的精确搜索与模糊搜索
  • electron 如何配置 打开控制台
  • Android 开发 获取Debug 跟 Release 包的SHA1值
  • DeepSeek16-open-webui Pipelines开发填坑
  • C语言再出发:2025年AI时代的关键语言
  • 华为云Flexus+DeepSeek征文|基于华为云一键部署 Dify-LLM 平台,结合 MCP 工具与 DeepSeek 模型打造智能学习助手
  • 【stm32】HAL库开发——Cube配置基本定时器
  • 猴子爬山(华为OD)
  • 什么是回归测试?什么时候需要做回归测试?
  • bug复盘:MCP SSE Client 生命周期问题之context.Background() 的使用
  • B站视频下载技术揭秘:从浏览器抓包到FFmpeg音视频合成
  • 0 数学习题本
  • GraphQL注入 -- GPN CTF 2025 Real Christmas
  • 开发语言漫谈-R语言
  • Apache 支持 HTTPS
  • Hive3.1.3加载paimon-hive-connector-3.1-1.1.1.jar报错UnsatisfiedLinkError
  • C++ Programming Language —— 第3章:运算符
  • HDFS(Hadoop分布式文件系统)总结
  • 【unitrix】 4.7 库数字取反(not.rs)
  • 组织策略性陪伴顾问
  • Java后端中的并发控制:从锁机制到无锁编程的实现
  • 供应链管理:主要生产计划类型及其相关信息
  • Vue-14-前端框架Vue之应用基础嵌套路由和路由传参
  • 【fish-speech】新模型openaudio-s1-mini尝鲜
  • 【windows处理技巧】如何缩小PDF
  • R语言机器学习算法实战系列(二十六)基于tidymodels的XGBoost二分类器全流程实战
  • 【力扣 困难 C】32. 最长有效括号