Home > Back-end >  How to implement AutoScroll HeaderedItemsControl WPF?
How to implement AutoScroll HeaderedItemsControl WPF?

Time:09-27

I merged the things I have found out to implement an auto-scroll-to-end Headered Items Control. I cannot manage to do it. What am I doing wrong?

In Resource Directory, ScrollingHeaderedItemsControl is styled as:

<Style TargetType="common:ScrollingHeaderedItemsControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type HeaderedItemsControl}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="3" SnapsToDevicePixels="True">
                    <StackPanel>
                        <Grid>
                            <Rectangle Fill="{TemplateBinding Background}"/>
                            <ContentPresenter ContentSource="Header" Margin="2,0,0,0"/>
                        </Grid>
                        <ScrollViewer VerticalScrollBarVisibility="Hidden">
                            <ItemsPresenter Margin="5,0,0,0"/>
                        </ScrollViewer>
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

The class ScrollingHeaderedItemsControl is defined as here:

public class ScrollingHeaderedItemsControl : HeaderedItemsControl
    {
        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                int newItemCount = e.NewItems.Count;
                if (newItemCount > 0)
                    this.ScrollToEnd();
                base.OnItemsChanged(e);
            }
        }
    }

ScrollToEnd is a static function written specifically for ScrollingHeaderedItemsControl such as:

public static void ScrollToEnd(this ItemsControl control)
        {

            try
            {
                Border border = VisualTreeHelper.GetChild((DependencyObject)control, 0) as Border;
                StackPanel sp = VisualTreeHelper.GetChild((DependencyObject)border, 0) as StackPanel;
                ScrollViewer sv = VisualTreeHelper.GetChild((DependencyObject)sp, 1) as ScrollViewer;
                sv.ScrollToEnd();
            }
            catch(Exception)
            {

            }
        }

ScrollingHeaderedItemsControl is used in the UserControl like this:

    <common:ScrollingHeaderedItemsControl x:Name="MessagesHIC" FontSize="32" Header="Error/Warning/Info Messages" 
                                                  Background="Green" 
                                      BorderBrush="AntiqueWhite" ItemsSource="{Binding Messages}"
                                      Margin="10" Grid.Row="1" Grid.Column="0"
                                                  ScrollViewer.CanContentScroll="True">
                <common:ScrollingHeaderedItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding MessageString}" Foreground="{Binding MessageColor}" TextWrapping="Wrap" FontWeight="Light" FontSize="26" />
                    </DataTemplate>
                </common:ScrollingHeaderedItemsControl.ItemTemplate
</common:ScrollingHeaderedItemsControl>

CodePudding user response:

Your template contains a stackpanel which doesn't set a height constraint, therefore the scrollviewer is never required to show the scrollbars. Change your Template to the following:

<Style TargetType="common:ScrollingHeaderedItemsControl">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type HeaderedItemsControl}">
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="3" SnapsToDevicePixels="True">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"></RowDefinition>
                                        <RowDefinition></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <Grid>
                                        <Rectangle Fill="{TemplateBinding Background}" />
                                        <ContentPresenter ContentSource="Header" Margin="2,0,0,0" />
                                    </Grid>
                                    <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Hidden">
                                        <ItemsPresenter Margin="5,0,0,0" />
                                    </ScrollViewer>
                                </Grid>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

You will then need to change your code behind to:

try
            {
                Border border = VisualTreeHelper.GetChild((DependencyObject)control, 0) as Border;
                Grid sp = VisualTreeHelper.GetChild((DependencyObject)border, 0) as Grid;
                ScrollViewer sv = VisualTreeHelper.GetChild((DependencyObject)sp, 1) as ScrollViewer;
                sv.ScrollToEnd();
            }
            catch (Exception)
            {

            }
  • Related