-1

It has been while since I worked with XAML on a regular basis and I am struggling with the basics.

I am trying to show items in an ItemsControl like so:

<DockPanel DockPanel.Dock="Left" Width="800">

    <TextBlock DockPanel.Dock="Top" Text="{Binding ProfilePages.Count}"></TextBlock>

    <Grid>
        <ItemsControl ItemsSource="{Binding ProfilePages}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="Hello World" Height="100" Width="200" Background="AliceBlue"></TextBlock>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

    </Grid>   

</DockPanel>

The ViewModel is just as basic:

public class XtmProjectViewModel : NotifyingObject
{
    private ViewModelCollection<XtmProfilePageViewModel, XtmProfilePage> _profilePages;

    public ViewModelCollection<XtmProfilePageViewModel, XtmProfilePage> ProfilePages
    {
        get { return _profilePages; }

        set
        {
            _profilePages = value;
            RaisePropertyChanged(() => ProfilePages);
        }
    }

    public ViewModelCollection<XtmSearchPageViewModel, XtmSearchPage> SearchPages { get; }

    public XtmProjectViewModel(XtmProject model)
    {
        ProfilePages = new ViewModelCollection<XtmProfilePageViewModel, XtmProfilePage>(model.ProfilePages, s => new XtmProfilePageViewModel(s));
        SearchPages = new ViewModelCollection<XtmSearchPageViewModel, XtmSearchPage>(model.SearchPages, s => new XtmSearchPageViewModel(s));
        ProfilePages.CollectionChanged += ProfilePages_CollectionChanged;
    }

    private void ProfilePages_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        Console.WriteLine("Test");
        RaisePropertyChanged(() => ProfilePages);
    }
}

ViewModelCollection is a custom type which synchs with the underlying collection of models automatically. I've used this for years in all types of scenarios with no problems.

However, in the view, the items don't show up and I get a weird behavior which I cannot explain:

  • The text block bound to ProfilePages.Count works as expected, i.e. the number showing up is the number of items in the list.
  • No binding errors
  • The CollectionChanged event of the ProfilePages-collection is fired correctly
  • Also adding a RaisePropertyChanged-event for the entire collection property in the CollectionChanged event handler doesn't change the behavior
  • The get accessors of the ProfilePages property is called twice as expected in the previous sceanrio (firing RaisePropertyChanged)
  • When I edit the XAML while debugging, sometimes items show up in the ItemsControl as expected. The list of items doesn't update afterwards, however

I cannot explain the behavior and have no idea, what the issue is. I have checked the common troubles (wrong definition of ItemTemplate, missing CollectionChanged event, layout bugs causing items to render invisibly, etc. with no success).

How can this behavior be explained? How can it be fixed?

Marc
  • 12,706
  • 7
  • 61
  • 97
  • wondering if you are inserting the objects into ProfilePages *not* on the UI thread. – kenny Mar 18 '19 at 10:42
  • My first thought was the `ViewModelCollection` is not of type `IEnumerable`? But you said it is showing items sometimes. Have you tried using `CollectionViewSource` in XAML? You could also create temp collection and then assign it to your `ProfilePages` property. – XAMlMAX Mar 18 '19 at 10:51
  • @kenny That was ist, thank you!! – Marc Mar 18 '19 at 11:00
  • @kenny Feel free to post a reply! I'll delete mine and mark as solved! – Marc Mar 18 '19 at 11:01
  • @Marc you're welcome. Pushed it to an answer. – kenny Mar 18 '19 at 11:03

1 Answers1

2

On request of the OP, moving my comment to an answer, 15000 here we come ;)

wondering if you are inserting the objects into ProfilePages not on the UI thread.

kenny
  • 21,522
  • 8
  • 49
  • 87