Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binding a usercontrol to the opposite of a bool property

Pretty straightforward: I'm looking to do the same as this but in winforms. Everything that google seems to pull up is wpf specific (ie. I don't want to reference presentationframework.dll)

Explained if you don't want to read the link:

The following is a representation of the intent of what I'd like to do, though it obviously doesn't work.

CheckBox1.DataBindings.Add(new Binding("Checked", this.object, "!SomeBool"));
like image 574
Steven Evers Avatar asked Mar 17 '10 16:03

Steven Evers


People also ask

How to bind a property in XAML?

One-Way Data Binding The following XAML code creates four text blocks with some properties. Text properties of two text blocks are set to “Name” and “Title” statically, while the other two text blocks Text properties are bound to “Name” and “Title” which are class variables of Employee class which is shown below.

What is binding why binding of data is necessary?

What is data binding? Data binding is the process that establishes a connection between the app UI and the data it displays. If the binding has the correct settings and the data provides the proper notifications, when the data changes its value, the elements that are bound to the data reflect changes automatically.

How does binding work in WPF?

Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.

How to bind data?

Select the first TextBox. Expand "Other data source" ---> "Project data source" --> "AdventureWorksdataset" --> "Employee" --> "EmloyeeId". Select the second TextBox. Expand the DataBinding property then select "Text" ---> "Employee Binding source" then select column(National ID) from the list.


1 Answers

You have two options:

  1. Create the Binding object manually and attach to the Format and Parse events and swap the value in each.
  2. Create an additional property on the class that just reverses the logic of the intended property

The first option is cleaner, IMO, as it doesn't force your class's API to follow your UI design, though the second option is (marginally) easier.

Example of Option 1

private void SwitchBool(object sender, ConvertEventArgs e)
{ 
    e.Value = !((bool)e.Value);
}

...

Binding bind = new Binding("Checked", this.object, "SomeBool");

bind.Format += SwitchBool;
bind.Parse += SwitchBool;

CheckBox1.DataBindings.Add(bind);

Example of Option 2

public class SomeClass
{
    public bool SomeBool { get; set; }

    public bool NotSomeBool
    {
        get { return !SomeBool; }
        set { SomeBool = !value; }
    }
}

...

CheckBox1.DataBindings.Add("Checked", this.object, "NotSomeBool");

Again, I very much favor option 1, since option 2 requires that you tailor your class to your UI design.

like image 151
Adam Robinson Avatar answered Oct 05 '22 23:10

Adam Robinson