我正在尝试扩展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
触发器的第二个实例ArgumentException
:This causes two bindings in the collection to bind to the same property.