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

WPF 特性------Binding

工业控制中,经常会需要把一个bool  型输入信号的状态显示在面板上,使用wpf 绑定的办法,可简洁实现:

实现步骤:

1,定义类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;namespace WpfAppBoolBinding
{public class MainViewModel : INotifyPropertyChanged{private bool _myProperty;public bool MyProperty{get { return _myProperty; }set{if (_myProperty != value){_myProperty = value;OnPropertyChanged();}}}public event PropertyChangedEventHandler PropertyChanged;protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = ""){if (this.PropertyChanged != null){this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));}}}public class TestViewModel{public MainViewModel MainView { get; set; }public int couter { get; set; }}
}

2,定义bool  类型转换器:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;namespace WpfAppBoolBinding
{[ValueConversion(typeof(bool), typeof(Brush))]public class BooleanToBrushConverter : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){return (bool)value ? Brushes.Green : Brushes.Red;}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotImplementedException();}}}

3,xml 实现:

<Window x:Class="WpfAppBoolBinding.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfAppBoolBinding" mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><local:BooleanToBrushConverter x:Key="BooleanToBrushConverter"/></Window.Resources><Grid><StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"><Ellipse Width="50" Height="50" Fill="{Binding MainView.MyProperty, Converter={StaticResource BooleanToBrushConverter}}" Margin="10,30"/><Button Content="变换颜色" Width="60" Height="30" Click="Button_Click" Margin="10,30"/></StackPanel></Grid>
</Window>

4,进行Datacontex 绑定:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace WpfAppBoolBinding
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{TestViewModel testViewModel = new TestViewModel();public MainWindow(){InitializeComponent();testViewModel.MainView = new MainViewModel();DataContext = testViewModel;}private void Button_Click(object sender, RoutedEventArgs e){testViewModel.MainView.MyProperty = !testViewModel.MainView.MyProperty;}}
}

http://www.lryc.cn/news/474877.html

相关文章:

  • 深入解析 FastAPI 查询参数:配置、类型转换与灵活组合
  • 大学城水电管理系统开发:Spring Boot指南
  • Lua 从基础入门到精通(非常详细)
  • [MySQL#11] 索引底层(2) | B+树 | 索引的CURD | 全文索引
  • 一个指针可以被声明为 `volatile`
  • [0260].第25节:锁的不同角度分类
  • android数组控件Textview
  • openpnp - 手工修改配置文件(元件高度,size,吸嘴)
  • Java 集合一口气讲完!(中)d=====( ̄▽ ̄*)b
  • 位运算:计算机科学中的基本操作
  • MPSK(BPSK/QPSK/8PSK)调制解调的Matlab仿真全套
  • 如何为STM32的EXTI(外部中断)编写程序
  • 八、快速入门Kubernetes之service
  • JVM 类加载机制详解
  • 在 JavaScript 中,`Array.prototype.filter` 方法用于创建一个新数组,该数组包含通过测试的所有元素
  • 63 mysql 的 行锁
  • ubuntu文件编辑操作
  • Nuxt.js 应用中的 nitro:config 事件钩子详解
  • 【前端】项目中遇到的问题汇总(长期更新)
  • DAY73WEB 攻防-支付逻辑篇篡改属性值并发签约越权盗用算法溢出替换对冲
  • 2024 Rust现代实用教程:Ownership与结构体、枚举
  • MMed-RAG:专为医学视觉语言模型设计的多功能多模态系统
  • 数据采集(全量采集和增量采集)
  • GPT-Sovits-1-数据处理
  • web前端多媒体标签设置(图片,视频,音频)以及图片热区(usemap)的设置
  • 尚硅谷react教程_扩展_stateHook
  • 专线物流公共服务平台:数据驱动,标准引领,共创金融双赢新时代
  • 界面控件DevExpress JS ASP.NET Core v24.1亮点 - 支持Angular 18
  • Spring之依赖注入(DI)和控制反转(IoC)——配置文件、纯注解
  • 基于SpringBoot的宠物健康咨询系统的设计与实现