Reputation: 17373
I've a two way data binding set up for Title Text box on win form as below:
txtTitle.DataBindings.Add("Enabled", Person, "Editable", true, DataSourceUpdateMode.OnPropertyChanged);
Unfortunately, when text is Disabled users can't copy the text. Is there any workaround to enabling the COPY/PASTE preserving two way data bind?
Upvotes: 2
Views: 2082
Reputation: 1346
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.
Upvotes: 1
Reputation: 814
Override the Enable property, set a local flag, ignore user input when keys are pressed by filtering by the local flag. And set colors to fake that it's disabled.
Upvotes: 0