WPF How to Override how ScrollViewer works in the code behind
So I made a WPF custom controller, that displays the thumbnails of images. Anyhow, I read the images from a folder and I do not know how many images are in the folder. So I can't display them all at one time because they'll kill my runtime memory. So I only wanted to display 18 at a time but I still want the vertical stroll bar to appear and I want the button to adjust like it would for the actual size of the number of images in the file.
So basically I want my ScrollViewer to work how it would normally work but only display 10 thumbnails controllers at a times, without actually reading the other ones from the file in to runtime memory so my computer will not crash.
here is my Xaml code :
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ThumbnailLib;component/ScrollViewerStyle.xaml"/>
<ResourceDictionary Source="/ThumbnailLib;component/ScrollBarTestStyle.xaml"/>
<ResourceDictionary Source="/ThumbnailLib;component/ScrollBarThumbVertical2Dictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="MainDisplayGrid" >
<Rectangle Fill="AliceBlue">
<Rectangle.Effect>
<DropShadowEffect Opacity="0.25" BlurRadius="25" RenderingBias="Quality" ShadowDepth="3" />
</Rectangle.Effect>
</Rectangle>
<ScrollViewer Name="ImageDisplayControlScroll" VerticalScrollBarVisibility="Visible" Template="{DynamicResource ScrollViewerControlStyle}" >
<ItemsControl Name="ImageDisplayControl" ScrollViewer.VerticalScrollBarVisibility="Visible" Width="auto" Height="auto">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="FrameworkElement.Margin" Value="10"/>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</ScrollViewer>
</Grid>
Here is my code behind :
private void UpdateThumbnailViewer(int numberOfThumbnailControls)
{
int maxVisibleThumbnails = 18;
int thumbnailsPerRow = 3;
double thumbnailControlHeight = 110;
if (numberOfThumbnailControls > maxVisibleThumbnails)
{
double scrollViewerHeight = maxVisibleThumbnails / thumbnailsPerRow * thumbnailControlHeight;
// Set the height of the ScrollViewer to show only the specified number of thumbnails
ImageDisplayControlScroll.Height = scrollViewerHeight;
// Show the vertical scrollbar
ImageDisplayControlScroll.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
// Scroll to the top of the ScrollViewer
ImageDisplayControlScroll.ScrollToTop();
Debug.WriteLine("VerticalScrollBarVisibility set to Visible");
}
else
{
// Hide the vertical scrollbar
ImageDisplayControlScroll.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
}
}
I am settings thumbnailsPerRow to 3 because 18 thumbnailControl of Height 110 can fit in to 3 row just fine.
But the vertical scrollbar button is not displaying at all.
How do I fix this ?