wpf ItemsControl binding problem

痞子三分冷 提交于 2020-01-07 04:52:07

问题


I bind to a ItemsControl in my codebehind:

ColumnVisibilityItems.DataContext = gc.ColumnVisibility;

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

my markup

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

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

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.


回答1:


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>


来源:https://stackoverflow.com/questions/3159128/wpf-itemscontrol-binding-problem

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!