WPF控件:密码框绑定MVVM

devtools/2024/9/24 7:01:54/

以下是一种使用 MVVM 模式的方法:

  1. 首先,在 ViewModel 中添加一个属性来保存密码,我们可以使用 SecureString 类型。
 // 密码变量private SecureString \_password;// 密码属性,用于获取和设置密码public SecureString Password{get{return \_password;}set{// 如果新值与旧值不同if (\_password != value){// 更新密码\_password = value;// 触发属性更改通知,通知UI层密码已更改RaisePropertyChanged(nameof(Password));}}}

  1. 创建一个附加属性来处理 PasswordBox 的密码变化,并将其绑定到 ViewModel 中的命令。
 public ICommand PasswordChangedCommand => new DelegateCommand<object>(PasswordChanged);private void PasswordChanged(object parameter){var passwordBox = parameter as PasswordBox;if (passwordBox != null){// 设置 ViewModel 中的密码属性Password = passwordBox.SecurePassword;}}

  1. 在 XAML 中,使用行为触发器来触发命令。
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<PasswordBoxx:Name="PasswordBox"Height="45"Margin="5"FontSize="20"FontWeight="Thin">"PasswordChanged">"{Binding PasswordChangedCommand}" CommandParameter="{Binding ElementName=PasswordBox}" />
  1. 查看密码框的内容。
MessageBox.Show(SecureStringToString(Password));
/// 
/// 将 SecureString 类型的数据转换为普通的字符串类型。
/// 
/// 要转换的 SecureString 对象。
/// 转换后的字符串,如果转换失败则返回空字符串。
private string SecureStringToString(SecureString secureString)
{// 初始化指针IntPtr ptr = IntPtr.Zero;try{// 将 SecureString 转换为指针ptr = Marshal.SecureStringToGlobalAllocUnicode(secureString);if (ptr != IntPtr.Zero){// 将指针中的数据复制到一个普通的字符串return Marshal.PtrToStringUni(ptr);}else{return string.Empty;}}catch (Exception ex){// 处理异常Console.WriteLine($"转换 SecureString 出错:{ex.Message}");return string.Empty;}finally{// 清除内存中的敏感数据if (ptr != IntPtr.Zero){Marshal.ZeroFreeGlobalAllocUnicode(ptr);}}
}


http://www.ppmy.cn/devtools/27211.html

相关文章

从MySQL+MyCAT架构升级为分布式数据库,百丽应用OceanBase 4.2的感受分享

本文来自OceanBase的客户&#xff0c;百丽时尚的使用和测试分享 业务背景 百丽时尚集团&#xff0c;作为国内大型时尚鞋服集团&#xff0c;在中国超过300个城市设有直营门店&#xff0c;数量超过9,000家。集团构建了以消费者需求为核心的垂直一体化业务模式&#xff0c;涵盖了…

练习题(2024/5/1)

1二叉树的层平均值 简单 相关标签 相关企业 给定一个非空二叉树的根节点 root , 以数组的形式返回每一层节点的平均值。与实际答案相差 10-5 以内的答案可以被接受。 示例 1&#xff1a; 输入&#xff1a;root [3,9,20,null,null,15,7] 输出&#xff1a;[3.00000,14.50000…

【Cortex-M3 CMSIS内核驱动文件详解】1:文件预置宏

文章目录 一、宏定义准备1.1 前置宏定义1.2 Cortex-M3存储器映射基地址宏1.3 硬件抽象层宏 一、宏定义准备 1.1 前置宏定义 #ifdef __cplusplusextern "C" { #endif #define __CM3_CMSIS_VERSION_MAIN (0x01) …

qt嵌入并控制外部程序

一、流程 1、调用Window接口模拟鼠标&#xff0c;键盘事件 POINT point; LPPOINT lpppoint &point; GetCursorPos(lpppoint);//获取鼠标位置 SetCursorPos(point.x, point.y);//设置鼠标位置//鼠标左键按下 mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, poi…

c++图论基础(2)

目录 图的存储方式&#xff1a; 邻接矩阵&#xff1a; 代码实现&#xff1a; 邻接表&#xff1a; 代码实现&#xff1a; 邻接矩阵邻接表对比&#xff1a; 带权图&#xff1a; 邻接矩阵存储&#xff1a; 邻接表存储(代码实现)&#xff1a; 图的存储方式&#xff1a; 邻…

关于NPM的Registry(npm源)

查看当前npm源&#xff1a; npm config get registry 官方源和淘宝源&#xff1a; ①官方源&#xff1a; npm config set registryhttp://registry.npmjs.org ②淘宝源&#xff1a; npm config set registryhttps://registry.npmmirror.com 据了解&#xff1a; 淘宝以前的源…

GPT3 探索指南(一)

原文&#xff1a;zh.annas-archive.org/md5/e19ec4b9c1d08c12abd2983dace7ff20 译者&#xff1a;飞龙 协议&#xff1a;CC BY-NC-SA 4.0 序言 如果这本书是由人工智能写的呢&#xff1f;你会阅读吗&#xff1f;我希望会&#xff0c;因为其中的部分确实是由人工智能写的。是的&…

在做题中学习(48):朴素的二分查找

. - 力扣&#xff08;LeetCode&#xff09; 解法一&#xff1a; 暴力求解 for循环中&#xff0c;从nums[0]枚举到nums[n-1]&#xff0c;依次判断&#xff0c;返回 target的值。 时间复杂度 : O(N) :因为要遍历一遍数组 解法二&#xff1a;二分查找 因为此数组为有序的…