Looping "complex" object with ItemsControl

129 views Asked by At

I have the following "element":

public class ReportElementViewModel
{
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                RaisePropertyChanged("Name");
            }
        }
    }

    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            if (_isChecked != value)
            {
                _isChecked = value;
                RaisePropertyChanged("IsChecked");
            }
        }
    }
}

My ViewModel contains many ReportElementViewModels:

public abstract class ReportViewModel<TPrimaryModel> : SharedViewModel
    where TPrimaryModel : Model, new()
{       
    public ObservableCollection<ReportElementViewModel> ReportElementViewModels
    {
        get { return _reportElementViewModels; }
        set
        {
            if (_reportElementViewModels != value)
            {
                _reportElementViewModels = value;
                RaisePropertyChanged("ReportElementViewModels");
            }
        }
    }
}

I removed the members to reduce code complexity but they are implemented correctly.

In my view I want to show all ReportElementViewModels by showing their Name and IsChecked (checkbox).

I thought the ItemsControl would be the right "tool", however it doesn't work (nothing is shown):

<ItemsControl ItemsSource="{Binding ReportElemetViewModels}" Height="auto" VerticalAlignment="Top" Grid.Row="1">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="269"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
1

There are 1 answers

2
Mike Eason On BEST ANSWER

You have made a spelling mistake in your Binding.

ItemsSource="{Binding ReportElemetViewModels}"

Should be:

ItemsSource="{Binding ReportElementViewModels}"