wpf ItemsControl绑定问题 [英] wpf ItemsControl binding problem

查看:137
本文介绍了wpf ItemsControl绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码后面绑定到ItemsControl:

I bind to a ItemsControl in my codebehind:

ColumnVisibilityItems.DataContext = gc.ColumnVisibility;

其中ColumnVisibility是ObservableCollection,也使用字典对其进行了尝试.

where ColumnVisibility is a ObservableCollection, also tried it with dictionary..

我的标记

                <ItemsControl x:Name="ColumnVisibilityItems">
                    <Label Content="{Binding Path=Name}" />
                </ItemsControl>

单步执行过程中,我看到集合绑定中包含11个项目.但是ItemsControl仅呈现集合中的第一个项目.

while stepping through, i see the collection bound having 11 items. but ItemsControl renders only the first item in collection.

是否需要设置ItemsSource属性才能使其正常工作?因为每当我尝试在后面的代码中设置它时,我都会得到一个例外,说项目无法修改,因为它们已经存在.

Is ItemsSource property necessary to be set for this to work? because whenever i try to set that in code behind, i get the exception saying items cannot be modified because they exist already.

推荐答案

基本上,您需要指定模板.有关完整示例,请参见 msdn文档

basically you need to specify your Template. See the msdn docs for a fuller example

<ItemsControl x:Name="ColumnVisibilityItems" ItemsSource="{Binding}>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding Path=Name}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这篇关于wpf ItemsControl绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆