WPF 特性------Binding

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

实现步骤:

1,定义类:

cs 复制代码
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 类型转换器:

cs 复制代码
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 实现:

cs 复制代码
<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 绑定:

cs 复制代码
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;
        }
    }
}
相关推荐
绿龙术士20 小时前
构建现代化WPF应用:数据驱动开发与高级特性解析
c#·wpf
wangnaisheng2 天前
【WPF】Opacity 属性的使用
wpf
姬激薄3 天前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)3 天前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心3 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo4 天前
9.1.领域驱动设计
wpf
大道随心4 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠4 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go4 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet4 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf