zniwalla
zniwalla

Reputation: 387

Binding in ListBox using MVVM

I have a problem binding all array entries to my ListBox in XAML.

XAML:

<ListBox ItemsSource="{Binding ResultFlag}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding TypeInfo}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

ResultFlag property in my ViewModel (which is the DataContext of the XAML file):

private ObservableCollection<DataField> _resultFlag;
public ObservableCollection<DataField> ResultFlag
{
    get { return _resultFlag; }
    set
    {
         _resultFlag = value;
         OnPropertyChanged();
    }
}

TypeInfo in the DataField class:

public string[] TypeInfo { get; set; }

I would like to show all string entries from above array in the ListBox - how should I do this? I've tried several things including nested Listbox and binding the ItemsSource of the ListBox directly to the array (didn't work, BTW)
Cheers!

Upvotes: 2

Views: 3006

Answers (1)

amuz
amuz

Reputation: 364

What you have in your scenario is a List of List. In order to show that in your list box you need to have nested ListBoxes like this.

       <ListBox ItemsSource="{Binding ResultFlag}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <ListBox ItemsSource="{Binding TypeInfo}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Upvotes: 1

Related Questions