1

我正在尝试扩展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是行不通的。

我错过了什么?

编辑:

我发现可能的问题是在创建数据绑定时未显示上下文菜单,但我找到的解决方案都不起作用。

编辑#2

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

4

1 回答 1

0

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

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

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

于 2013-09-23T16:59:56.857 回答