我为赢表上的“标题文本”框设置了两种方式的数据绑定,如下所示:
txtTitle.DataBindings.Add("Enabled", Person, "Editable", true, DataSourceUpdateMode.OnPropertyChanged);
不幸的是,当文本被禁用时,用户无法复制文本。是否有任何变通办法来启用COPY / PASTE保留两种方式的数据绑定?
文本框控件具有ReadOnly属性,您可以将其设置为true。绑定仍应更新。
选项1:使用NotifyPropertyChanged创建一个类
创建一个包含将要绑定的数据的类:
public class Book : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
[Bindable(true)]
public string Name
{
get { return name; }
set
{
name = value;
if (PropertyChanged != null)
PropertyChanged(this,
new PropertyChangedEventArgs("Name"));
}
}
public Book(string name)
{
Name = name;
}
}
创建此类的实例并绑定它:
public Book TheBook { get; set; }
public Form1()
{
InitializeComponent();
TheBook = new Book("");
textBox1.DataBindings.Add(new Binding("Text",
TheBook,
"Name",
true,
DataSourceUpdateMode.OnPropertyChanged,
""));
}
更改属性,它将更新:
private void button1_Click(object sender, EventArgs e)
{
TheBook.Name = "Different";
}
选项2:创建一个自定义控件以将Enabled屏蔽为ReadOnly
创建以下自定义控件,并将其用于绑定到Enabled:
public partial class DBTextBox : TextBox
{
private bool enabled;
public new bool Enabled
{
get { return enabled; }
set
{
enabled = value;
base.Enabled = true;
ReadOnly = !enabled;
}
}
public DBTextBox()
{
InitializeComponent();
}
}
每当该DBTextBox设置为Enabled = false时,它将改为ReadOnly。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句