给绑定数据源的文本输入框TextEdit.Text或EditValue属性赋值,失去焦点文本自动清空了
作者:C/S框架网|www.cscode.ne  发布日期:2020-03-19 23:04:32
  给绑定数据源的文本输入框TextEdit.Text或EditValue属性赋值,失去焦点文本自动清空了

给绑定数据源的文本输入框TextEdit.Text或EditValue属性赋值,失去焦点文本自动清空了

被清空的原因:

当文本输入框组件绑定数据源时,直接給Text, EditValue属性赋值是无效的,因为没有写入数据到数据源。

必须使用DataBinding.WriteValue方法写入数据。


解决方案1:赋值后,使用WriteValue方法。

C# Code:

//模拟给TextEdit赋值
private void button1_Click(object sender, EventArgs e)
{
  
//textEdit1.Text = "www.CSFramework.com";
  textEdit1.EditValue
= "www.CSFramework.com";
  
  
//使用此方法正常!
  
//WriteValue:读取控件属性的当前值并将其写入数据源
  
textEdit1.DataBindings[0].WriteValue();
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761



解决方案2:封装1个自定义方法赋值,比如SetEditorBindingValue。


C# Code:

//模拟给TextEdit赋值
private void button2_Click(object sender, EventArgs e)
{
  
//textEdit1.EditValue = "www.CSFramework.com";
  
//textEdit1.Text = "www.CSFramework.com";
  

  SetEditorBindingValue(textEdit1,
"www.CSFramework.com");
}

/// <summary>
/// 给绑定数据源的组件赋值
/// </summary>
/// <param name="bindingControl"></param>
/// <param name="value"></param>
protected void SetEditorBindingValue(BaseEdit bindingControl, object value)
{
  
//bindingControl.Text = value;
  
bindingControl.EditValue = value;
  
  
if (bindingControl.DataBindings.Count > 0)
  {
    bindingControl.DataBindings[
0].WriteValue();
  }
}

//来源:C/S框架网 | www.csframework.com | QQ:23404761



C/S系统快速开发框架绑定数据源的自定义方法:


C# Code:

/// <summary>
/// 绑定输入控件的数据源
/// </summary>
/// <param name="edit">控件框</param>
/// <param name="dataSource">数据源</param>
/// <param name="bindField">取值字段</param>
public void BindingTextEditBase(BaseEdit edit, object dataSource, string bindField)
{
  
try
  {
    edit.DataBindings.Clear();
    Binding b
= new Binding("EditValue", dataSource, bindField);
    edit.DataBindings.Add(b);
    b.ReadValue();
  }
  
catch (Exception ex)
  {
    
//
    
}
  }
  
  
//来源:C/S框架网 | www.csframework.com | QQ:23404761



贴图图片-给绑定数据源的文本输入框TextEdit赋值



C# Code:

public partial class Form1 : Form
{
  
public Form1()
  {
    InitializeComponent();
  }
  
  
/// <summary>
  
/// 绑定输入控件的数据源
  
/// </summary>
  
/// <param name="edit">控件框</param>
  
/// <param name="dataSource">数据源</param>
  
/// <param name="bindField">取值字段</param>
  
public void BindingTextEditBase(BaseEdit edit, object dataSource, string bindField)
  {
    
try
    {
      edit.DataBindings.Clear();
      Binding b
= new Binding("EditValue", dataSource, bindField);
      edit.DataBindings.Add(b);
      b.ReadValue();
    }
    
catch (Exception ex)
    {
      
//
      
}
    }
    
    
private void Form1_Load(object sender, EventArgs e)
    {
      MyCustomer customer
= new MyCustomer
      {
        CustomerCode
= "CSFramework.com",
        CustomerName
= "C/S框架网"
        };
        
        
//绑定数据源
        
BindingTextEditBase(textEdit1, customer, "CustomerCode");
        BindingTextEditBase(textEdit2, customer,
"CustomerName");
      }
      
      
public class MyCustomer
      {
        
public string CustomerName { get; set; }
        
public string CustomerCode { get; set; }
      }
      
      
//模拟给TextEdit赋值
      
private void button1_Click(object sender, EventArgs e)
      {
        
        textEdit1.EditValue
= "www.CSFramework.com";
        
        
//使用此方法正常!
        
//WriteValue:读取控件属性的当前值并将其写入数据源
        
textEdit1.DataBindings[0].WriteValue();
      }
      
      
//模拟给TextEdit赋值
      
private void button2_Click(object sender, EventArgs e)
      {
        
//textEdit1.EditValue = "www.CSFramework.com";
        
//textEdit1.Text = "www.CSFramework.com";
        

        SetEditorBindingValue(textEdit1,
"www.CSFramework.com");
      }
      
      
/// <summary>
      
/// 给绑定数据源的组件赋值
      
/// </summary>
      
/// <param name="bindingControl"></param>
      
/// <param name="value"></param>
      
protected void SetEditorBindingValue(BaseEdit bindingControl, object value)
      {
        
//bindingControl.Text = value;
        
bindingControl.EditValue = value;
        
        
if (bindingControl.DataBindings.Count > 0)
        {
          bindingControl.DataBindings[
0].WriteValue();
        }
      }
      
      
private void textEdit1_EditValueChanged(object sender, EventArgs e)
      {
        
//
        
}
      }
      
      
//来源:C/S框架网 | www.csframework.com | QQ:23404761



C/S框架网|原创精神.创造价值.打造精品

扫一扫加微信:
C/S框架网作者微信 C/S框架网|原创作品.质量保障.竭诚为您服务

上一篇 下一篇