2

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?

1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This won't work, winform has been setup as two way data bind. Need to sync ReadOnly and Enable which is not possible as DataBinding doesn't support ReadOnly
@Myagdi I edited my code to reflect more on binding. Does this help?
@Measuring I tried this on my side and it looks to be working. I'll remove my suggestion as it doesn't look like it works when binding is enabled.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.