Fluffy
Fluffy
CC#
Created by Fluffy on 1/9/2024 in #help
Scrollviewer not staying at the bottom when new insert. MVVM .NET Frameworks 4.5 XAML
I have the following xaml. This works fine, HOWEVER, whenever a new message is inserted into the ItemsControl my scrollviewer doesn't update and stays in plays. This means that new messages can't be seen unless your scroll down manually and I cannot for the life of me figure out why it won't stay in its relative position.
<ItemsControl.ItemTemplate>

<DataTemplate>

<Border x:Name="MessageBorder" MinHeight="40" MinWidth="280" BorderThickness="1"
Background="#EFEBE9" BorderBrush="#BCAAA4"
Margin="10,0,60,10" CornerRadius="4" SnapsToDevicePixels="True" HorizontalAlignment="Left">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="15"/>
</Grid.RowDefinitions>

<TextBlock x:Name="MessageTxtBlock" Grid.Row="1" Margin="7,5,7,0" TextWrapping="Wrap"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Text="{Binding Message}"/>

<TextBlock Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Stretch"
Margin="0,0,5,0" FontSize="10" Opacity="0.8"
Text="{Binding Time, StringFormat={}{0:t}}"/>
</Grid>
</Border>

<DataTemplate.Triggers>
omitted for brevity
</DataTemplate.Triggers>

</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<ScrollViewer x:Name="MessageScroller" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemTemplate>

<DataTemplate>

<Border x:Name="MessageBorder" MinHeight="40" MinWidth="280" BorderThickness="1"
Background="#EFEBE9" BorderBrush="#BCAAA4"
Margin="10,0,60,10" CornerRadius="4" SnapsToDevicePixels="True" HorizontalAlignment="Left">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="15"/>
</Grid.RowDefinitions>

<TextBlock x:Name="MessageTxtBlock" Grid.Row="1" Margin="7,5,7,0" TextWrapping="Wrap"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Text="{Binding Message}"/>

<TextBlock Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Stretch"
Margin="0,0,5,0" FontSize="10" Opacity="0.8"
Text="{Binding Time, StringFormat={}{0:t}}"/>
</Grid>
</Border>

<DataTemplate.Triggers>
omitted for brevity
</DataTemplate.Triggers>

</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<ScrollViewer x:Name="MessageScroller" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
4 replies