A textbox control has a ReadOnly property which you can set to true. Binding should still be updated.
Option 1: Create a class with NotifyPropertyChanged
Create a class holding the data that will be bound:
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;
}
}
Create an instance of this class and bind it:
public Book TheBook { get; set; }
public Form1()
{
InitializeComponent();
TheBook = new Book("");
textBox1.DataBindings.Add(new Binding("Text",
TheBook,
"Name",
true,
DataSourceUpdateMode.OnPropertyChanged,
""));
}
Change the property and it will update:
private void button1_Click(object sender, EventArgs e)
{
TheBook.Name = "Different";
}
Option 2: Create a custom control to mask Enabled as ReadOnly
Create the following custom control and use that for binding to 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();
}
}
Whenever that DBTextBox is set to Enabled = false it will make it ReadOnly instead.