1

I've an usercontrol with a groupbox where there's an itemscontrol in it. Within the itemscontrol I put an scrollviewer. When I run the application it isn't showing all the items. Sometimes I see 1 or 2 items, sometimes nothing. I'am certain that my list has at least 10 items in it. What am I doing wrong here?

I've put the scrollviewer in the itemscontrol like described here: WPF: ItemsControl with scrollbar (ScrollViewer)

 <UserControl x:Class="Hermes.UI.HistoryListControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml    /presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Hermes.UI"
             mc:Ignorable="d" 
             d:DesignHeight="500" d:DesignWidth="800" d:DataContext="{x:Static local:HistoryListDesignModel.Instance}">

    <GroupBox Margin="5 0"
              Padding="4"
              BorderBrush="{StaticResource clr_dark_brush}"
              BorderThickness="0.5">

        <GroupBox.Header>
            <Label Content="Geschiedenis"
                   FontFamily="{StaticResource NunitoBold}"
                   FontSize="{StaticResource FontSizeRegular}"
                   Foreground="{StaticResource clr_dark_brush}"/>
        </GroupBox.Header>


        <ItemsControl ItemsSource="{Binding Items}">
            <ItemsControl.Template>
                <ControlTemplate>
                    <ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}">
                        <ItemsPresenter />
                    </ScrollViewer>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <local:HistoryListItemControl />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

    </GroupBox>

</UserControl>

and hen the local:HistoryListItemControl

<UserControl x:Class="Hermes.UI.HistoryListItemControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Hermes.UI"
         mc:Ignorable="d" d:DesignWidth="800">

<UserControl.Resources>
    <Style x:Key="contentstyle" TargetType="{x:Type ContentControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContentControl}">

                    <Border x:Name="background" 
                            Background="White"
                            Margin="0 5">

                        <Grid x:Name="container" Background="Transparent">

                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <TextBlock Text="{Binding date_cal, StringFormat='dd MMMM yyyy'}"
                                   FontSize="{StaticResource FontSizeLarge}"
                                   FontFamily="{StaticResource NunitoThin}"
                                   Foreground="{StaticResource clr_info_brush}"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="Center"/>

                            <Grid Grid.Row="1">

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <TextBlock FontFamily="{StaticResource FontAwesome}"   
                                       VerticalAlignment="Center" 
                                       Margin="5"
                                       Text="&#xf5b7;" />
                                <TextBlock Text="{Binding Device_name}"
                                       VerticalAlignment="Center"
                                       Grid.Column="1"/>

                                <TextBlock FontFamily="{StaticResource FontAwesome}"   
                                       VerticalAlignment="Center" 
                                       Margin="5"
                                       Text="&#xf275;"
                                       Grid.Column="2"/>
                                <TextBlock Text="{Binding Unit_name}"
                                       VerticalAlignment="Center"
                                       Grid.Column="3"/>

                            </Grid>

                            <Grid Grid.Row="2">

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <TextBlock FontFamily="{StaticResource FontAwesome}"   
                                       VerticalAlignment="Center" 
                                       Margin="5"
                                       Text="&#xf192;" />
                                <TextBlock Text="{Binding Sensor}"
                                       VerticalAlignment="Center"
                                       Grid.Column="1"/>

                                <TextBlock FontFamily="{StaticResource FontAwesome}"   
                                       VerticalAlignment="Center" 
                                       Margin="5"
                                       Text="&#xf240;"
                                       Grid.Column="2"/>
                                <TextBlock Text="{Binding Battery}"
                                       VerticalAlignment="Center"
                                       Grid.Column="3"/>

                            </Grid>

                            <Grid Grid.Row="3">

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <TextBlock FontFamily="{StaticResource FontAwesome}"   
                                       VerticalAlignment="Center" 
                                       Margin="5"
                                       Text="&#xf086;" />
                                <TextBlock Text="{Binding Comments}"
                                       VerticalAlignment="Center"
                                       Grid.Column="1"/>

                            </Grid>

                        </Grid>

                    </Border>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<ContentControl d:DataContext="{x:Static local:HistoryListItemDesignModel.Instance}"
                Style="{StaticResource contentstyle}" />

</UserControl>

The items are loaded in with a viewmodel and is called from within the constructor of the viewmodel:

 private void GetHistory()
        {
            HistoryBLL historyBLL = new HistoryBLL();
            HistoryDataTable myHistory = historyBLL.GetHistoryByDeviceId(Device.Id);

            foreach (HistoryRow row in myHistory)
            {
                UnitsBLL unitsBLL = new UnitsBLL();

                Items.Add(new HistoryListItemViewModel
                {
                    Date_cal = (DateTime)row.date_cal,
                    Device_name = row.tagnaam,
                    Unit_name = unitsBLL.GetUnitById(row.unit_id).unit,
                    Sensor = row.sensor_id.ToString(),
                    Battery = (row.battery) ? "Ja" : "",
                    Comments = row.comments
                });

                unitsBLL = null;
            }
        }

When I put a breakpoint at the last line I can see that the List of Items is filling up with at least 10 items, so that part is working.

I expect to see all the items and not just 1 or 2 (of even nothing)

Siegfried
  • 63
  • 5
  • Do you have any bindings failing in your output window? Is Items really binding? What does it contain and what is it? We need more info. – Andy May 11 '19 at 16:08
  • It seems odd that you copied `x:Name="ScrollViewer" Padding="{TemplateBinding Padding}"` from that answer without any obvious reason. Anyway, did you try your XAML with some basic control like a TextBlock instead of your HistoryListItemControl in the ItemTemplate? – Clemens May 11 '19 at 17:12
  • @Andy no I've no warnings or errors concerning item binding. – Siegfried May 12 '19 at 08:11
  • @Clemens I've updated my original post a bit, like u see I've made an design view model and in the designer its working like it should be. I've a likewise page and there it is working perfectly, so I don't know why not in this case... – Siegfried May 12 '19 at 08:12

0 Answers0