【问题标题】:WPF itemscontrol binding to collection of controlsWPF itemscontrol 绑定到控件集合
【发布时间】:2010-12-16 20:11:29
【问题描述】:

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

<ItemsControl ItemsSource="{Binding CustomFields}">

还有代码:

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

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

感谢您的帮助

【问题讨论】:

    标签: wpf data-binding itemscontrol


    【解决方案1】:

    以下属性与您使用的 XAML 一样:

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

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

    【讨论】:

    • 谢谢,将 ObservableCollection 更改为 UIElement 类型而不是控件有效。抱歉,我还不能投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多