Jamie Keeling
Jamie Keeling

Reputation: 9966

Access WPF control programmatically

The problem with this is probably obvious but im struggling to see it.

I have the following XAML:

<ItemsControl x:Name="contentList">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Name}" TextWrapping="Wrap" />
                    <ItemsControl x:Name="imageContent" Grid.Column="1">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding ImageCollection.FullName}" TextWrapping="Wrap" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

I've set up the itemsSource for contentList like so:

contentList.ItemsSource = myObservableCollection;

However when I try to do the same for imageContent, I can't seem to access it via IntelliSense. I've tried a clean/rebuild of the Project and it's made no difference.

Do I need to access imageContent a different way?

I want to use myObservableCollection for both contentList and imageContent as it has the following structure:

With an aim to produce the following UI:

enter image description here

Upvotes: 2

Views: 313

Answers (1)

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26048

You need to define another ObservableCollection within the list objects of your outer collection. Something like this:

ObservableCollection<MyObject> OuterList = new ObservableCollection<MyObject>();

//...


public class MyObject
{
    public ObservableCollection<FileInfo> ImageCollection {get; set;}
    public MyObject()
    {
        ImageCollection = new ObservableCollection<FileInfo>();
    }
}

Then just update your xaml like so:

...
<ItemsControl x:Name="imageContent" ItemsSource="{Binding ImageCollection}">
...

So this will cause each item in your outer list to hold it's own observable collection holding it's list.

Also with this change make sure you update the binding on your text block, since each item will represent a FileInfo object you can simply write this:

<DataTemplate>
    <TextBlock Text="{Binding FullName}" TextWrapping="Wrap" />
</DataTemplate>

Upvotes: 2

Related Questions