WinForms - 如何在应用程序的设置中保存组合框选择?

时间:2013-07-28 14:12:19

标签: c# winforms binding controls application-settings

我在工具条中嵌入了一个组合框 - 一个ToolStripCombobox实例。

项目列表是enum的值列表。

我希望能够加载/保存选择(其中一个Selected[Index|Item|Text|...]属性,从/到应用的Settings“机制”。

理想情况下,我希望能够从设计师那里做到这一点。

通常情况下,将控件的属性挂钩到某个设置(在设计器中)来自控件的属性(ApplicationSettings)下 - 但SelectedXXX属性中没有一个显示在那里。

FWIW,在特定于toostrip的组合框中,SelectedXXX实际上toolStripComboInstance.ComboBox.SelectedXXX属性实际上更深一些。

到目前为止我所做的是在代码中配置绑定:

m_runTypeCombo   //the toolstrip control
    .ComboBox    //the actual combobox
    .DataBindings.Add(
    new System.Windows.Forms.Binding(
        "SelectedItem", 
        global::JavaPad.Properties.Settings.Default, 
        "RunType", 
        true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged
    )
);

上述工作,但我希望有更清洁的东西(即基于设计师)。如果内置的ToolStripCombobox不支持这个,那么是否有一种(简单的)方法从中派生我自己的类型,并以这样的方式公开SelectedXXX属性,使其与< em>应用程序设置基础架构(及其在设计器中的支持)?

1 个答案:

答案 0 :(得分:1)

如果您愿意将TooltipComboBox包装在您自己的自定义控件中,您可以这样做:

public class MyCombo : ToolStripComboBox
{
    [SettingsBindable(true)]
    public int SelectedIndex
    {
        get { return ComboBox.SelectedIndex; }
        set { ComboBox.SelectedIndex = value; }
    }


}

请注意,除了确认我可以将控件添加到ToolStrip以及我可以选择属性之外,我还没有对此进行测试 - 您可能需要添加PropertyChanged事件以使其完全正常工作。

相关问题