1

I'm trying to extend ContextMenuStrip (I'm reusing it quite often) so it'd be bind with FormSettings (which automatically store settings later). For example whether to overwrite or append file when exporting something.

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" );
            }
        }
    }
}

My extended context menu strip:

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
    }
}

But when I try to debug DataBindings all bindings have IsBinding set to false and BindingManagerBase is also null. So biding to FormSettings doesn't work.

What am I missing?

Edit:

I found that the possible issue is in context menu is not displayed at the time data binding is created, but none of solutions I found works.

Edit #2

If I make FormSettings bindable and add bindings on its side it works as expected until I try to create second instance of ContextMenuStrip which triggers ArgumentException: This causes two bindings in the collection to bind to the same property.

Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96

1 Answers1

0

I found working workaround about this problem (this will cause DataBidings to be initialized when all controls are set for the form):

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

It works but it's just kludge... If anyone has better (more systematic) solution I'll be glad.

Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • 2
    `DataBinding` in `winforms` is very complicated, sometimes you just know **this works** but you can't explain **why it works**. – King King Sep 23 '13 at 17:30