绑定属性来控制的WinForms

编程入门 行业动态 更新时间:2024-10-26 14:29:45
本文介绍了绑定属性来控制的WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以,当属性值发生变化时,它控制的绑定属性改变什么是一个属性绑定到一个控制的最佳途径。

What is the best way to bind a property to a control so that when the property value is changed, the control's bound property changes with it.

所以,如果我有一个属性姓我要绑定到一个文本框的 txtFirstName 文本值。所以,如果我改变姓价值堆栈,那么属性 txtFirstName.Text 也变成值堆栈。

So if I have a property FirstName which I want to bind to a textbox's txtFirstName text value. So if I change FirstName to value "Stack" then the property txtFirstName.Text also changes to value "Stack".

我知道这听起​​来可能愚蠢的问题,但我会AP preciate的帮助。

I know this may sound a stupid question but I'll appreciate the help.

推荐答案

您必须实施 INotifyPropertyChanged的键,添加绑定到文本框。

You must implement INotifyPropertyChanged And add binding to textbox.

我公司将提供C#code片段。希望它能帮助

I will provide C# code snippet. Hope it helps

class Sample : INotifyPropertyChanged { private string firstName; public string FirstName { get { return firstName; } set { firstName = value; InvokePropertyChanged(new PropertyChangedEventArgs("FirstName")); } } #region Implementation of INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged; public void InvokePropertyChanged(PropertyChangedEventArgs e) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, e); } #endregion }

用法:

Sample sourceObject = new Sample(); textbox.DataBindings.Add("Text",sourceObject,"FirstName"); sourceObject.FirstName = "Stack";

更多推荐

绑定属性来控制的WinForms

本文发布于:2023-10-08 08:55:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472126.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   属性   WinForms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!