数据绑定如果为false则启用

时间:2012-10-18 18:41:40

标签: c# data-binding

我在winforms-application中遇到数据绑定问题。 在下面的代码中,我有一个数据绑定到文本框的enabled属性。启用状态取决于复选框的值。

tbAmount.DataBindings.Add("Enabled", checkBox, "Checked", 
                          false, DataSourceUpdateMode.OnPropertyChanged);
如果选中该复选框,则在此代码中

启用文本框。但我需要它倒置。如果取消选中该复选框,我希望启用文本框。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:3)

这应该这样做。

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

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

    textBox.DataBindings.Add(bind);

答案 1 :(得分:0)

我知道这是一篇很老的文章,但是多年来,我一直在寻找类似的东西,但对最终使用的东西却从未真正感到满意。迈克·帕克(Mike Park)的回答很好,不仅因为它可行,而且因为它很简单。

我所做的只是接听Mike的回答,并将其转换为Control扩展。谢谢迈克!

取决于在何处以及如何使用它,您可能需要添加对System.Windows.Forms的引用和一条使用System.Windows.Forms的语句。

    /// <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.Bindings.Add(YourButton.UncheckedBinding(YourCheckBox)); 
    /// 
    /// 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.Bindings.Add(YourButton.UncheckedBinding("Enabled", YourCheckBox)); 
    /// 
    /// 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;
    }