Search code examples
wpfdata-bindingitemscontrol

WPF itemscontrol binding to collection of controls


I'm trying to bind to a collection of controls I generate dynamically:

<ItemsControl ItemsSource="{Binding CustomFields}">

And the code:

    public ObservableCollection<Control> CustomFields
    {
        get
        {
            return GetCustomFields();
        }
    }

The Getcustomfields just generates some controls like a ComboBox, textbox etc. The binding seems to work, but the window doesn't show any of my controls. Probably because I need an datatemplate in the itemscontrol. My question is what kind of datatemplate do I need?

Thanks for any help


Solution

  • The following property like with the same XAML as you use:

    public ObservableCollection<UIElement> Controls
    {
        get
        {
            var collection = new ObservableCollection<UIElement>();
            collection.Add(new Button { Content = "Hello world" });
    
            return collection;
        }
    }
    

    Maybe your problem comes from somewhere else... Can you give us the code needed to reproduce the problem ?