首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果为false,则启用数据绑定

问如果为false,则启用数据绑定
EN

Stack Overflow用户
提问于 2012-10-19 02:41:40
回答 2查看 2.7K关注 0票数 6

在winforms应用程序中,我遇到了一个数据绑定问题。在下面的代码中,我将数据绑定到textbox的enabled-属性。启用状态取决于复选框的值。

代码语言:javascript
复制
tbAmount.DataBindings.Add("Enabled", checkBox, "Checked", 
                          false, DataSourceUpdateMode.OnPropertyChanged);

在此代码中,如果复选框处于选中状态,则会启用textbox。但我要把它倒过来。如果复选框未选中,我希望启用textbox。我如何才能做到这一点?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-19 03:11:03

这个应该可以了。

代码语言:javascript
复制
    Binding bind = new Binding("Enabled", checkBox, "Checked");

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

    textBox.DataBindings.Add(bind);
票数 5
EN

Stack Overflow用户

发布于 2020-05-07 07:30:03

我知道这是一个非常古老的帖子,但这些年来我一直在寻找类似的东西,并且对我最终使用的东西从未真正感到满意。Mike Park的答案很棒,不仅因为它有效,而且因为它非常简单。

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

根据您使用它的位置和方式,您可能需要添加对System.Windows.Forms的引用和一条using System.Windows.Forms语句。

代码语言:javascript
复制
    /// <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;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12961533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档