WPF项目控制绑定到控件集合

Elg*_*des 3 data-binding wpf itemscontrol

我正在尝试绑定到动态生成的控件集合:

<ItemsControl ItemsSource="{Binding CustomFields}">
Run Code Online (Sandbox Code Playgroud)

和代码:

    public ObservableCollection<Control> CustomFields
    {
        get
        {
            return GetCustomFields();
        }
    }
Run Code Online (Sandbox Code Playgroud)

Getcustomfields只生成一些控件,如ComboBox,文本框等.绑定似乎工作,但窗口不显示我的任何控件.可能是因为我需要在itemscontrol中使用datatemplate.我的问题是我需要什么样的数据模板?

谢谢你的帮助

jap*_*apf 5

以下属性与您使用的XAML相同:

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

        return collection;
    }
}
Run Code Online (Sandbox Code Playgroud)

也许你的问题来自其他地方......你能给我们重现问题所需的代码吗?