【问题标题】:Binding.IsBinding = false and Biding.BindingManagerBase = null on ContextMenuStripContextMenuStrip 上的 Binding.IsBinding = false 和 Biding.BindingManagerBase = null
【发布时间】:2013-09-23 14:49:58
【问题描述】:

我正在尝试扩展ContextMenuStrip(我经常重复使用它),因此它将与FormSettings 绑定(稍后会自动存储设置)。例如导出内容时是否覆盖或附加文件。

表单设置:

internal sealed partial class FormSettings : global::System.Configuration.ApplicationSettingsBase, INotifyPropertyChanged
{

    private static FormSettings defaultInstance = ((FormSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized( new FormSettings() )));


    private void OnPropertyChanged( string propertyName )
    {
        this.OnPropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
    }

    /// <summary>
    /// Default instance
    /// </summary>
    public static FormSettings Default
    {
        get { return defaultInstance; }
    }

    private FormSettings()
        : base( "column_settings" )
    {
    }

    ~FormSettings()
    {
        defaultInstance.Save();
    }

    [Bindable( true )]
    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute( "False" )]
    public bool ExportCSVOverwrite
    {
        get { return (bool)this["ExportCSVOverwrite"]; }
        set
        {
            if (value != (bool)this["ExportCSVOverwrite"]) {
                this["ExportCSVOverwrite"] = value;
                OnPropertyChanged( "ExportCSVOverwrite" );
            }
        }
    }
}

我的扩展上下文菜单条:

public class MinimalExample : ContextMenuStrip, INotifyPropertyChanged
{
    bool _export_overwrite = false;

    /// <summary>
    /// Triggered when some property gets changed
    /// </summary>
    [Browsable( true )]
    [Category( "Action" )]
    public event PropertyChangedEventHandler PropertyChanged;

    [Browsable( true )]
    [Category( "Behavior" )]
    [Bindable( true )]
    public bool ExportOverWrite
    {
        get { return _export_overwrite; }
        set
        {
            if (value != _export_overwrite) {
                _export_overwrite = value;
                OnPropertyChanged( "ExportOverWrite" );
            }
        }
    }

    /// <summary>
    /// Triggered when property gets changed
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    virtual protected void OnPropertyChanged( object sender, PropertyChangedEventArgs e )
    {
        if (PropertyChanged != null) {
            PropertyChanged( sender, e );
        }
    }

    /// <summary>
    /// Triggered when property gets changed
    /// </summary>
    /// <param name="property_name"></param>
    protected void OnPropertyChanged( string property_name )
    {
        OnPropertyChanged( this, new PropertyChangedEventArgs( property_name ) );
    }

    public MinimalExample()
    {
        var tmp = DataBindings.Add( "ExportOverWrite", FormSettings.Default, "ExportCSVOverwrite", 
                           false, DataSourceUpdateMode.OnPropertyChanged, false );
        // Breakpoint here
    }
}

但是当我尝试调试DataBindings 时,所有绑定都将IsBinding 设置为false 并且BindingManagerBase 也是null。所以对FormSettings的投标是行不通的。

我错过了什么?

编辑:

我发现可能的问题是在创建数据绑定时未显示上下文菜单,但没有一个解决方案 I found 有效。

编辑#2

如果我使 FormSettings 可绑定并在其一侧添加绑定,它会按预期工作,直到我尝试创建触发 ArgumentExceptionContextMenuStrip 的第二个实例:This causes two bindings in the collection to bind to the same property.

【问题讨论】:

    标签: c# winforms data-binding


    【解决方案1】:

    我找到了解决此问题的方法(这将导致在为表单设置所有控件时初始化DataBidings):

        protected override void OnOpened( EventArgs e )
        {
            base.OnOpened( e );
            DataBindings.Clear();
            InitializeDataBindings();
        }
    

    它有效,但它只是杂乱无章......如果有人有更好(更系统)的解决方案,我会很高兴。

    【讨论】:

    • DataBinding in winforms 非常复杂,有时您只知道这是有效的,但无法解释为什么有效
    猜你喜欢
    • 1970-01-01
    • 2015-02-22
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    相关资源
    最近更新 更多