0
목표는 BindingSource + Object를 DataSource로, TextBox를 사용하여 객체의 속성 값을 표시하는 것입니다.bindingsource를 통해 객체 속성에 텍스트 상자를 바인딩하지 않고 새로 고침
내가 직면 한 문제는 TextBox가 기본 개체의 속성 값 변경을 반영하지 않는다는 것입니다.
중국인이 말하기를 한 그림은 천개의 단어와 같으므로 내가 직면하는 문제를 보여주는 데모 코드 아래에 있습니다. btnAssignDataSource 버튼을 클릭하면
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
namespace TextBoxDataSourceBindingDemo
{
public partial class Form1 : Form
{
private BindingSource dsSource;
private BindingDemoClass bDemoClass;
public Form1()
{
InitializeComponent();
bDemoClass = new BindingDemoClass();
dsSource = new BindingSource();
bDemoClass.BindingName = "DemoBinding";
this.textBox1.DataBindings.Add(new Binding("Text", dsSource, "BindingName",true,DataSourceUpdateMode.OnPropertyChanged));
}
private void btnAssignDataSource_Click(object sender, EventArgs e)
{
//Setting the datasource is not enough to
//update the related textbox
//refresh must be explicity called ?
dsSource.DataSource = bDemoClass;
dsSource.ResetBindings(true);
}
private void btnChangePropertyValue_Click(object sender, EventArgs e)
{
//Here after setting the property value
//the textbox should be update with the new value; correct ?
bDemoClass.BindingName = "DemoBinding2";
}
}
//Demo class used as datasource
public class BindingDemoClass : INotifyPropertyChanged
{
private string _BindingName = String.Empty;
public string BindingName
{
get { return _BindingName; }
set
{
if (!String.Equals(_BindingName, value))
{
_BindingName = value;
NotifyPropertyChanged();
}
}
}
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
}
그래서 텍스트 상자가 업데이트됩니다. 바운드 속성이 변경되면 (btnChangePropertyValue) 변경 사항이 텍스트 상자에 반영됩니다.
예 동일한 결과가 나타납니다. – Yiorgos
btnChangePropertyValue 이벤트를 호출 한 후 바인딩을 재설정 할 수 있습니까? – sagar
그래, 처음부터 시도했지만 작동하지 않았다. 이 작업을 수행하는 유일한 방법은 텍스트 상자에서 바인딩을 제거하고 다시 추가하는 것입니다. 속성 이벤트 변경이 발생하여 파트가 작동합니다. 아마도 뭔가가 텍스트 상자에 바인딩과 관련이 있습니다. – Yiorgos