WPF中的多重绑定

MultiBinding 将会给后端传回一个数组, 其顺序为绑定的顺序. 例如:

        <DataGridMargin="10"AutoGenerateColumns="False"ItemsSource="{Binding Stu}"><DataGrid.Columns><DataGridTextColumn Binding="{Binding Id}" Header="Id" /><DataGridTextColumn Binding="{Binding Name}" Header="Name" /><DataGridTextColumn Binding="{Binding Age}" Header="Age" /><DataGridTextColumn Binding="{Binding Description}" Header="Description" /><DataGridTemplateColumn><DataGridTemplateColumn.CellTemplate><DataTemplate><ButtonWidth="60"HorizontalAlignment="Center"Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.MyButtonCommand}"CommandParameter="{Binding}"Content="申请"><Button.Style><Style TargetType="Button"><!--<Setter Property="IsEnabled" Value="{Binding Age, Converter={StaticResource SingleParamConverter}}" />--><Setter Property="IsEnabled"><Setter.Value><MultiBinding Converter="{StaticResource MultiParamConverter}"><Binding Path="Age"/><Binding Path="Id"/></MultiBinding></Setter.Value></Setter></Style></Button.Style></Button></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid>

在这里的 Button 的isEnabled属性用了多重绑定给converter, 用来筛选条件

                                        <Setter Property="IsEnabled"><Setter.Value><MultiBinding Converter="{StaticResource MultiParamConverter}"><Binding Path="Age"/><Binding Path="Id"/></MultiBinding></Setter.Value></Setter>

这时 后端转换器为:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;namespace NavTest.Components
{public class MultiParamConverter : IMultiValueConverter{public object Convert(object[] values,Type targetType,object parameter,CultureInfo culture){int age;int id;if (values == null){return true;}int.TryParse(values[0].ToString(), out age);int.TryParse(values[1].ToString(), out id);if (age > 1 && id > 5){return true;}return false;}public object[] ConvertBack(object value,Type[] targetTypes,object parameter,CultureInfo culture){throw new NotImplementedException();}}
}

效果:

在这里插入图片描述
完整代码:

view:

<UserControlx:Class="NavTest.Views.Page5"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:cv="clr-namespace:NavTest.Components"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:hc="https://handyorg.github.io/handycontrol"xmlns:i="http://schemas.microsoft.com/xaml/behaviors"xmlns:local="clr-namespace:NavTest.Views"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:mv="clr-namespace:NavTest.ViewModels"d:DataContext="{d:DesignInstance mv:Page5ViewModel}"d:DesignHeight="450"d:DesignWidth="800"mc:Ignorable="d"><UserControl.Resources><cv:SingleParamConverter x:Key="SingleParamConverter" /><cv:MultiParamConverter x:Key="MultiParamConverter" /></UserControl.Resources><Grid><Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition /></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition /><RowDefinition /></Grid.RowDefinitions><DataGridMargin="10"AutoGenerateColumns="False"ItemsSource="{Binding Stu}"><DataGrid.Columns><DataGridTextColumn Binding="{Binding Id}" Header="Id" /><DataGridTextColumn Binding="{Binding Name}" Header="Name" /><DataGridTextColumn Binding="{Binding Age}" Header="Age" /><DataGridTextColumn Binding="{Binding Description}" Header="Description" /><DataGridTemplateColumn><DataGridTemplateColumn.CellTemplate><DataTemplate><ButtonWidth="60"HorizontalAlignment="Center"Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.MyButtonCommand}"CommandParameter="{Binding}"Content="申请"><Button.Style><Style TargetType="Button"><!--<Setter Property="IsEnabled" Value="{Binding Age, Converter={StaticResource SingleParamConverter}}" />--><Setter Property="IsEnabled"><Setter.Value><MultiBinding Converter="{StaticResource MultiParamConverter}"><Binding Path="Age"/><Binding Path="Id"/></MultiBinding></Setter.Value></Setter></Style></Button.Style></Button></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid></Grid>
</UserControl>

viewModel:

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using NavTest.Eneities;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;namespace NavTest.ViewModels
{public partial class Page5ViewModel:ObservableObject{public Page5ViewModel(){for (int i = 0; i < 10; i++){Stu.Add(new(){Id = i + 2,Age = $"{i}",Name = $"Name{i}",Description = $"Description{i}"});}}[ObservableProperty]private ObservableCollection<Student> stu = new();[RelayCommand]public void MyButton(Student s){MessageBox.Show(s.Name);}}
}

转换器:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;namespace NavTest.Components
{public class MultiParamConverter : IMultiValueConverter{public object Convert(object[] values,Type targetType,object parameter,CultureInfo culture){int age;int id;if (values == null){return true;}int.TryParse(values[0].ToString(), out age);int.TryParse(values[1].ToString(), out id);if (age > 1 && id > 5){return true;}return false;}public object[] ConvertBack(object value,Type[] targetTypes,object parameter,CultureInfo culture){throw new NotImplementedException();}}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;namespace NavTest.Components
{public class SingleParamConverter : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){if (value == null){return true;}int age;int.TryParse(value.ToString(), out age);if (age > 5){return true;}return false;}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotImplementedException();}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/104437.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

bash一行输入,多行回显demo脚本

效果图&#xff1a; 脚本&#xff1a; #!/bin/bash # 定义一个变量&#xff0c;用来存储输入的内容 input"" # 定义一个变量&#xff0c;用来存储输入的字符 char""# 为了让read能读到空格键 IFS_store$IFS IFS# 提示内容&#xff0c;在while循环中也有&a…

three.js入门 —— 实现第一个3D案例

前言&#xff1a; three.js入门&#xff0c;根据文档实现第一个3D案例 效果图&#xff1a; 代码实现&#xff1a; const scene new THREE.Scene();//创建一个长方体几何对象Geometryconst geometry new THREE.BoxGeometry(100, 100, 100);//创建一个网络基础材质的材质对象…

机器人革命:脑洞大开的前沿机器人技术!

原创 | 文 BFT机器人 01 由生物启发的多模式移动形态机器人 在一个不断运动的世界中&#xff0c;一种新开发的名为M4&#xff08;多模式移动形态机器人&#xff09;的机器人展示了在包括滚动、飞行和行走在内的八种不同运动模式之间切换的能力。这款机器人由加州理工学院自主…

Eclipse中常用的操作单词

Eclipse中常用的操作单词 Existing Project into Workspace 现有的工程引入工作空间(加载工程) File 文件 New 新建 Project 工程 Folder 文件夹 Import 引入(加载&导入) Run As 运行 Applicatio…

Java系列之:深入理解设计模式

Java系列之:深入理解设计模式 一、设计模式相关技术文章二、设计原则三、设计模式概念四、设计模式的分类五、创建性模式六、创建性模式-工厂方法模式七、创建性模式-抽象工厂模式八、创建性模式-构建器模式九、面向对象设计-结构性模式十、结构性模式-适配器模式十一、结构性…

SOLIDWORKS® 2024 新功能 - 3D CAD

1、 先前版本的兼容性 • 利用您订阅的 SOLIDWORKS&#xff0c;可将您的 SOLIDWORKS 设计作品保存为旧版本&#xff0c;与使用旧版本 SOLIDWORKS 的供应商无缝协作。 • 可将零件、装配体和工程图保存为新版本前两年之内的SOLIDWORKS 版本。 优点&#xff1a; 即使其他用户正…

conda 创建虚拟环境

1.为什么要创建虚拟环境 我们在做开发或者跑论文实验可能会同时进行多个任务&#xff0c;这些任务可能会依赖于不同的python环境&#xff0c;比如有的用到3.6有的用到3.7&#xff0c;这时我们创建不同版本的python&#xff0c;放到虚拟环境中给不同的任务分别提供其所需要的版本…

腾讯云短信服务申请, api测试,发送含字母短信

参考&#xff1a;https://blog.csdn.net/weixin_49001740/article/details/125236893 另外补充&#xff1a;申请的模板中含有”验证码“ 的字&#xff0c; 模板变量 {1} 会被限制&#xff0c;只能发送0-6位的纯数字内容&#xff0c; 如果要发送带有字母的内容&#xff0c;就不要…

Git相关知识(1)

目录 1.初识Git 1.基础知识 2.centos中下载 2.基本操作 1.创建本地仓库 2.配置本地仓库 3.版本库、工作区、暂存区 4.添加文件 5.add和commit对git文件的作用 6.修改文件 7.版本回退 8.撤销修改 9.删除文件 3.分支操作 1.HEAD与分支 2.创建分支 3.删除分支 …

灿芯股份将上会:计划募资6亿元,董事长、总经理均为外籍

10月11日&#xff0c;上海证券交易所披露的信息显示&#xff0c;灿芯半导体&#xff08;上海&#xff09;股份有限公司&#xff08;下称“灿芯股份”&#xff09;将于10月18日接受上市审核委员会审议会议的现场审议。目前&#xff0c;该公司已递交了招股书&#xff08;上会稿&a…

Elasticsearch6实践

目录 目录 一、需求 二、ES索引设计 三、页面搜索条件 四、ES的分页搜索DSL语句 五、其他 一、需求 公告列表&#xff0c;需要支持以下搜索 1、根据文本输入&#xff0c;模糊搜索公告标题和公告正文。 2、支持公告类型搜索&#xff0c;单选 3、支持根据公告所在省市区搜…

基于SSM的班级事务管理系统

基于SSM的班级事务管理系统 开发语言&#xff1a;Java数据库&#xff1a;MySQL技术&#xff1a;SpringSpringMVCMyBatisVue工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 前台界面 登录界面 班委界面 学生界面 管理员界面 摘要 基于SSM&#xff08;Spring、Spring…

ARM-day9作业

main.c: #include "uart.h"#include "key_it.h"int main(){char c;char *s;uart4_init(); //串口初始化//中断初始化key_it_config();key3_it_config();//完成GPIO相关初始化all_led_init();//风扇初始化fs_init();//蜂鸣器初始化fmq_init();while(1){…

矿区井下智慧用电安全监测解决方案

一、背景 矿区井下作业具有复杂的环境和较高的危险性&#xff0c;对于用电安全的要求尤为严格。传统的管理模式和监测方法往往无法实时、准确地掌握井下用电情况&#xff0c;对安全隐患的排查与预防存在一定局限性。因此&#xff0c;引入智慧用电安全监测解决方案&#xff…

黑马JVM总结(三十二)

&#xff08;1&#xff09;类加载器-线程上下文1 使用的应用程序类加载器来完成类的加载&#xff0c;不是用的启动类加载器&#xff0c;jdk在某些情况下要打破&#xff0c;双亲委派的模式&#xff0c;有时候需要调用应用程序类加载器来完成类的加载&#xff0c;否则有些类它是找…

YB4058是一款经济高效、完全集成的高输入电压单电池锂离子电池充电器

高输入电压充电器支持I2C和OVP保护 概述&#xff1a; YB4058是一款经济高效、完全集成的高输入电压单电池锂离子电池充电器。充电器使用了锂离子电池所需的CC/CV充电曲线。充电器可接受高达27V的输入电压&#xff0c;但当输入电压超过OVP时禁用阈值&#xff0c;通常为6.8V&am…

比较完整一些chatGPT项目代码(权威)

https://gitee.com/zccbbg/chatgpt-springboot-service yml中的配置文件无法读取&#xff0c;前端访问比较困难。

倒置边框半径卡片

效果展示 CSS 知识点 实现多曲面的思路 实现整体布局 <div class"card"><div class"img_box"></div><div class"content"><div class"price"></div></div> </div>.card {position…

Vue动态绑定class

目录 绑定对象 绑定数组 用在组件上 组件内只有一个根元素 组件内有多个元素 class与动态class是可以一起使用的 绑定对象 :class "{ 类名1: 判断条件1, 类名2: 判断条件2, ... }" 如果类名后面对应的条件成立&#xff0c;则类名就会添加 案例 <template&…

Idea执行Pom.xml导入jar包提示sun.misc.BASE64Encoder jar找不到---SpringCloud工作笔记197

奇怪之前都是好好的,这个是因为,jdk的版本不对,重新打开以后自动被选择成jdk11了...记录一下 原因是从jdk9的时候,这个jar包已经被删除了,所以会报错,如果你用的是jdk自带的这个jar包就会报错,那么还可以,修改,不让他用jdk的,让他用 用org.apache.commons.codec.binary.Base64…