首页 > 其他分享 >Winform数据绑定基类

Winform数据绑定基类

时间:2024-07-03 14:32:57浏览次数:1  
标签:PropertyChanged propertyName 绑定 storage System value 基类 using Winform

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;


public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private readonly SynchronizationContext _syncContext;
private readonly TaskScheduler _uiTaskScheduler;
public BindableBase()
{
_syncContext = WindowsFormsSynchronizationContext.Current;
_uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
}

protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value))
{
return false;
}

storage = value;
RaisePropertyChanged(propertyName);
return true;
}

protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value))
{
return false;
}

storage = value;
onChanged?.Invoke();
RaisePropertyChanged(propertyName);
return true;
}

protected virtual void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
//PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(propertyName)));
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}

protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
//if (PropertyChanged != null)
//{
// _syncContext.Post(new SendOrPostCallback(_ =>
// {
// PropertyChanged(this, args);
// }), null);
//}

Task.Factory.StartNew(() =>
{
PropertyChanged(this, args);
}, CancellationToken.None, TaskCreationOptions.None, _uiTaskScheduler);
//this.PropertyChanged?.Invoke(this, args);
}
}
***************************************************使用方法**************************************************

直接继承基类,在属性Set中使用RaisePropertyChanged事件

标签:PropertyChanged,propertyName,绑定,storage,System,value,基类,using,Winform
From: https://www.cnblogs.com/HomeSapiens/p/18281562

相关文章

  • asp.net Core中控制器的两个基类ApiController和Controller
    1、Controller就是我们平常建的控制器  publicclassStudyontroller:Controller。2、ApiController这个我平常很少使用,今天发现了研究了一下,值得学习,一个很不错的控制器基类。   新建一个ApiController的步骤:  请求对应的方法就是通过请求提交数据的方式......
  • DevExpress WinForms磁贴导航面板 & TileBar组件,让桌面应用触摸更友好!
    界面控件DevExpressWinFormsTileNavPane被设计为位于应用程序窗口的顶部(就像Ribbon一样),可以被认为是Windows桌面应用程序中传统导航元素的触摸友好版本。P.S:DevExpressWinForms拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能......
  • 开发界的良心评测文章,告诉你WPF 与 WinForms 对比:性能、开发难度、特点及操作系统支持
    引言WindowsPresentationFoundation(WPF)和WindowsForms(WinForms)是微软提供的两种用于构建Windows桌面应用程序的框架。尽管它们都有助于开发高效的桌面应用程序,但在性能、开发难度、特点和操作系统支持方面存在显著差异。本文将详细比较WPF和WinForms,以帮助开发者选择......
  • 已解决java.rmi.NotBoundException:RMI中没有绑定的对象的正确解决方法,亲测有效!!!
    已解决java.rmi.NotBoundException:RMI中没有绑定的对象的正确解决方法,亲测有效!!!目录问题分析出现问题的场景服务器端代码客户端代码报错原因解决思路解决方法1.绑定远程对象2.检查查找名称3.验证RMI注册表启动RMI注册表完整示例代码服务器端代码客户端代码......
  • 48、k8s-安全认证-授权管理-角色创建、用户绑定角色
    授权发生在认证成功之后、同i过认证就可以知道请求用户是谁、然后kubernetes会根据事先定义的授权策略来决定用户是否有权限访问、这个过程就称为授权每个发送到APIServer的请求都带上了用户和资源的信息:比如发送请求的用户、请求的路径、请求的动作等、授权就是根据这些信息和......
  • Centos双网卡冗余绑定
    1.前言关于双网卡绑定,前前后后踩过不少的坑,虽然这是RHCE中的一道题,但是在实践中碰到问题也够喝一壶的。在实践中,虚拟机、物理机都做过,但是不尽相同,大部分的坑也集中在这里,本文长期更新关于网卡绑定中遇到的问题及处理方法。现在的服务器默认都配备4张千兆网卡,在生产环境......
  • 转:在Linux上运行WinForm
    C#winform软件实现一次编译,跨平台windows和linux、mac兼容运行,兼容VisualStudio原生界面Form表单开发-亲善美-博客园(cnblogs.com) 一、背景:微软的.netcore开发工具,目前来看,winform界面软件还没有打算要支持linux系统下运行的意思,要想让c#桌面软件在linux系统上运行,开......
  • C#winform如何在窗体实现视频播放
    1.在Form窗体设计中的左侧工具箱列表中右击鼠标,单击选择项。2.在弹出的对话框中点击COM组件,选择WindowsMediaPlayer,点击确定3.接着在工具箱选择WindowsMediaPlayer控件,将控件添加到窗体中,并且添加一个OpenFileDialog控件4.接着在工具箱的对话框中将OpenFileDialog添加......
  • 关于之前写的动态字符串的绑定再说一点
    1、我今天发现一个奇怪的事情我之前写的关于动态字符串的绑定https://www.cnblogs.com/guchen33/p/18060276<TextBlockWidth="200"Height="30"FontSize="20"Text="{BindingContent,StringFormat={}{0}!}"/> pu......
  • Winform RichTextBox 获取Text文本中段落及区块
    在C#WinForms应用程序中,RichTextBox控件是一个功能强大的文本编辑控件,支持多种文本格式。如果你需要获取RichTextBox中每一部分的文本,包括段落和不同样式的区块,可以通过以下步骤实现。总体思路是使用RichTextBox的RichTextBox.Find以及RichTextBox.SelectionStart和RichTextBox.......