Usercontrol contains a combobox, but the Items set at design time are not in the run time

wpf combobox selecteditem
wpf combobox set selected item
wpf combobox binding
wpf combobox binding selecteditem
wpf combobox events
wpf combobox binding to list of strings
wpf combobox add items dynamically
wpf combobox mvvm

I have a user control that contains a textbox and a combobox. I have exposed the combobox's Item property to clients of the user control, thus:

public System.Windows.Forms.ComboBox.ObjectCollection Item
{
    get { return baseComboBox.Items; }
}

I added the user control to a windows form, and set the Items list using the property values editor in the form designer. I then ran the application, and the combobox's drop down list was empty. To confirm that the items added at design time were not in the list, I added the following two lines of code to the client form:

textBox1.Text = userControl1.Items.Count.ToString();
userControl1.Items.Add("Test item");

When I re-ran the application the test box showed a count of 0 (zero), and the drop-down list of the user control contained only "Test item".

Thinking that maybe the instance of the user control being referenced at design time is a different instance from that being referenced at run time, I set the user control's BackColor property at design time. When I re-ran the app, the user control's BackColor was what I had set it to in the designer.

Any ideas on why the design time setting of the Items does not carry over into the run time?

You need an attribute:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ComboBox.ObjectCollection Item {
  get { return baseComboBox.Items; }
}

ComboBox in a usercontrol and using it at design time, I have a usercontrol written in code (not xaml) so that i am able to This usercontrol contains a combobox. from the dropdownlist, just as it works when you run the application. the whole element is in some kind of sleep state at design time. get { return (int)GetValue(ComboBoxIndexProperty); } set� In previous articles we explained WPF ComboBox with Image, Display Image From Folder or Current Directory, Export GridView to PDF, Remove Default Mouse Over Effect on WPF Buttons, etc and now we will move on Set property of ComboBox within UserControl instance. In this article we create one usercontrol and set the combobox in it.

Maybe you need the setter defined also, something like:

public System.Windows.Forms.ComboBox.ObjectCollection Item
{
    get { return baseComboBox.Items; }
    set { baseComboBox.Items = value; }
}

Or, maybe it's because you are exposing Item but setting Items

c#, i have user control contains textbox , combobox. have exposed any ideas on why design time setting of items not carry on run time? you need� -The ComboBox is using another Table which contains all the names of the different Tables I want shown in the Datatable. From what I understood I need either to create a collection or setup the values manually in the ComboBox Items. The problem here is you declare them as "Text" values so the DataTable's Items cannot process it.

Defining Set as a simple baseComboBox.Items = value; is impossible because baseComboBox.Items is defined as ReadOnly. The problem is also the lack of a defined editor for such a collection.

Therefore, you should add the editor definition and instead of trying to replace the collection as one object - use AddRange:

    [Editor("System.Windows.Forms.Design.StringCollectionEditor, " +
        "System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
        typeof(UITypeEditor))]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ComboBox.ObjectCollection Items
    {
        get
        {
            return baseComboBox.Items;
        }
        set
        {
            baseComboBox.Items.Clear();
            baseComboBox.Items.AddRange(value.Cast<object>().ToArray());
        }
    }

The ComboBox control, The ComboBox control is in many ways like the ListBox control, but takes up a lot less space, because the list of items is hidden when not needed. items of a ComboBox control is easy using XAML, but you will likely soon run set to true), you can read the Text property to know the value the user has entered or selected. We can create a ComboBox control using a Forms designer at design-time or using the ComboBox class in C# code at run-time. To create a ComboBox control at design-time, you simply drag and drop a ComboBox control from Toolbox to a Form in Visual Studio. After you drag and drop a ComboBox on a Form, the ComboBox looks like Figure 1.

Resources, If the resource is then changed later on, this change will not be reflected where also allows you to use resources which are not even there during design time, e.g. Resources> <sys:String x:Key="ComboBoxTitle">Items:</sys:String> <x: Array This time, we've added a couple of extra resources, so that our Window now� In Windows forms, ComboBox provides two different features in a single control, it means ComboBox works as both TextBox and ListBox. In ComboBox, only one item is displayed at a time and the rest of the items are present in the drop-down menu.

Combo Box Controls Tutorial, Use this control when you want auto-completion but do not need the extra It contains a Display property for the display text and a Value property for the value of user control combo boxes with the exception of non-value-type objects added You can set the filter at design-time by selecting the property and entering the� 1) when I add new connection at design time, I cant remove them (they disappear from the list but still display at run-time). 2) when adding items at run-time, they don't show in the list. 3) will probably have more issues but these are the main ones at present.

Control - Image Combo Box, Similar to standard combo but with ability to include pictures with each item in Unlike regular combo boxes, which may be populated at design time using their List tab, an Image Combo box is populated with items programmatically at run time. Set to None (default) if the combo box does not accept OLE drops and is to� The ComboBox is the User Control's Content, and the ComboBox's template for displaying individual items is overridden for displaying instances of SolidColorBrush as a 20x20 rectangle. The ComobBox is then bound to an Array of SolidColorBrushes created as a resource within the User Control. There is absolutely nothing exciting about this.

Comments
  • Steve, thanks for the response. Note that "Item" was a typo, and it doesn't really matter what the property name is, as long as the correct property on the combo box is being set. I could have named the user control's property "George", and, as long as the logic returns baseComboBox.Items, it will work. Of course, that might confuse any programmer who has to work with the user control! lol.