WPF项控件绑定到控件集合

3

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

<ItemsControl ItemsSource="{Binding CustomFields}">

代码如下:

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

Getcustomfields只是生成一些控件,比如ComboBox、文本框等。绑定似乎是有效的,但窗口没有显示任何控件。可能是因为我需要在itemscontrol中使用datatemplate。我的问题是我需要什么样的datatemplate?

感谢任何帮助。

1个回答

5
以下属性与您使用的相同XAML类似:
public ObservableCollection<UIElement> Controls
{
    get
    {
        var collection = new ObservableCollection<UIElement>();
        collection.Add(new Button { Content = "Hello world" });

        return collection;
    }
}

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

1
谢谢,将ObservableCollection更改为UIElement类型而不是Control类型确实起作用了。很抱歉,我还不能投票。 - Elger Mensonides

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接