views:

45

answers:

1

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.

+2  A: 

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>
qntmfred
i thought so, and actually tried messing with ItemTemplate, but it didn't seem to make a big difference. For example this code, produces one empty item... am i setting the binding incorrectly in codebehind?
Sonic Soul
i've updated my example to show how you would set ItemsSource, which is probably what you need
qntmfred
yep, that was it, thanks!
Sonic Soul