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

时间:2009-11-15 18:45:22

标签: wpf data-binding itemscontrol

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

<ItemsControl ItemsSource="{Binding CustomFields}">

代码:

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

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

感谢您的帮助

1 个答案:

答案 0 :(得分:5)

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

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

        return collection;
    }
}

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