Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Databinding Enabled if false

I have a problem with databinding in a winforms-application. In the following code i have a databinding to the enabled-property of a textbox. The enabled-state depends on the value of a checkbox.

tbAmount.DataBindings.Add("Enabled", checkBox, "Checked", 
                          false, DataSourceUpdateMode.OnPropertyChanged);

in this code the textbox is enabled if the checkbox is checked. but i need it inverted. i want the textbox to be enabled if the checkbox is unchecked. How can i achieve this?

like image 301
Tomtom Avatar asked Oct 18 '12 18:10

Tomtom


2 Answers

This should do it.

    Binding bind = new Binding("Enabled", checkBox, "Checked");

    bind.Format += 
        (sender, e) => 
            e.Value = !((bool)e.Value); // invert the checked value

    textBox.DataBindings.Add(bind);
like image 158
Mike Park Avatar answered Oct 21 '22 16:10

Mike Park


I know this is a very old post, but I have looked for something similar many times over the years and was never really happy with what I ended up using. Mike Park's answer is great, not only because it works, but because of how simple it is.

All I did was take Mike's answer and turn it into a Control extension. Thanks Mike!

Depending on where and how you use it, you may need to add a reference to System.Windows.Forms and a using System.Windows.Forms statement.

    /// <summary>
    /// Creates a DataBinding that allows you to bind to the Unchecked state instead of the normal Checked state.
    /// 
    /// Sample usage: In this case, I am enabling a Button when the CheckBox is unchecked.
    ///     // Defaults to Control Enabled property.
    ///     // Always bound to the DataSource Checked property.
    ///     YourButton.DataBindings.Add(YourButton.UncheckedBinding(YourCheckBox)); 
    /// 
    ///     var binding = YourButton.UncheckedBinding(YourCheckBox);
    ///     YourButton.DataBindings.Add(binding);
    /// 
    /// Adapted - from answer by Mike Park answered Oct 18 '12 at 19:11
    /// From: Databinding Enabled if false
    /// Link: https://stackoverflow.com/questions/12961533/databinding-enabled-if-false
    /// </summary>
    /// <typeparam name="T">Constrained to be a type that inherits from ButtonBase. This includes CheckBoxes and RadionButtons.</typeparam>
    /// <param name="control">The control that will consume the DataBinding.</param>
    /// <param name="DataSource">The control to which we are binding. We will always bind to the Checked property.</param>
    /// <returns>DataBinding that is bound to the Unchecked state instead of the usual Checked state.</returns>
    public static Binding UncheckedBinding<T>(this Control control, T DataSource) where T : ButtonBase
    {
        return UncheckedBinding(control, "Enabled", DataSource);
    }

    /// <summary>
    /// Creates a DataBinding that allows you to bind to the Unchecked state instead of the normal Checked state.
    /// 
    /// Sample usage: In this case, I am enabling a Button when the CheckBox is unchecked.
    ///     // Always bound to the DataSource Checked property.
    ///     YourButton.DataBindings.Add(YourButton.UncheckedBinding("Enabled", YourCheckBox)); 
    /// 
    ///     var binding = YourButton.UncheckedBinding(YourCheckBox);
    ///     YourButton.DataBindings.Add(binding);
    ///
    /// Adapted - from answer by Mike Park answered Oct 18 '12 at 19:11
    /// From: Databinding Enabled if false
    /// Link: https://stackoverflow.com/questions/12961533/databinding-enabled-if-false
    /// </summary>
    /// <typeparam name="T">Constrained to be a type that inherits from ButtonBase. This includes CheckBoxes and RadionButtons.</typeparam>
    /// <param name="control">The control that will consume the DataBinding.</param>
    /// <param name="DataSource">The control to which we are binding. We will always bind to the Checked property.</param>
    /// <param name="PropertyName">The name of the property that is being bound.</param>
    /// <returns>DataBinding that is bound to the Unchecked state instead of the usual Checked state.</returns>
    public static Binding UncheckedBinding<T>(this Control control, string PropertyName, T DataSource) where T : ButtonBase
    {
        var binding = new Binding(PropertyName, DataSource, "Checked");
        binding.Format += (sender, e) => e.Value = !((bool)e.Value);

        return binding;
    }
like image 37
j2associates Avatar answered Oct 21 '22 15:10

j2associates