Skip to content

Commit c61fd2c

Browse files
author
LoneWandererProductions
committed
test object
1 parent aa8e23a commit c61fd2c

3 files changed

Lines changed: 191 additions & 0 deletions

File tree

CommonControls/ThumbnailItem.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.ComponentModel;
2+
using System.Runtime.CompilerServices;
3+
using System.Windows.Media.Imaging;
4+
5+
namespace CommonControls
6+
{
7+
public class ThumbnailItem : INotifyPropertyChanged
8+
{
9+
private bool _isSelected;
10+
private BitmapSource _imageSource;
11+
12+
public int Id { get; set; }
13+
public string FilePath { get; set; }
14+
15+
// The actual image to display
16+
public BitmapSource ImageSource
17+
{
18+
get => _imageSource;
19+
set { _imageSource = value; OnPropertyChanged(); }
20+
}
21+
22+
// Two-way binding for the CheckBox
23+
public bool IsSelected
24+
{
25+
get => _isSelected;
26+
set { _isSelected = value; OnPropertyChanged(); }
27+
}
28+
29+
public event PropertyChangedEventHandler PropertyChanged;
30+
private void OnPropertyChanged([CallerMemberName] string name = null)
31+
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
32+
}
33+
}

CommonControls/Thumbnails.xaml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<UserControl x:Class="CommonControls.Thumbnails"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800">
7+
8+
<Grid>
9+
<ListBox x:Name="MainList"
10+
ItemsSource="{Binding ThumbCollection}"
11+
SelectionMode="Extended"
12+
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
13+
14+
<ListBox.ItemsPanel>
15+
<ItemsPanelTemplate>
16+
<WrapPanel Orientation="Horizontal" IsItemsHost="True" />
17+
</ItemsPanelTemplate>
18+
</ListBox.ItemsPanel>
19+
20+
<ListBox.ItemTemplate>
21+
<DataTemplate>
22+
<Border BorderThickness="2" CornerRadius="3" Margin="5"
23+
Width="{Binding DataContext.ThumbCellSize, RelativeSource={RelativeSource AncestorType=UserControl}}"
24+
Height="{Binding DataContext.ThumbCellSize, RelativeSource={RelativeSource AncestorType=UserControl}}">
25+
26+
<Border.Style>
27+
<Style TargetType="Border">
28+
<Setter Property="BorderBrush" Value="Transparent"/>
29+
<Style.Triggers>
30+
<DataTrigger Binding="{Binding IsSelected}" Value="True">
31+
<Setter Property="BorderBrush" Value="Blue"/>
32+
</DataTrigger>
33+
</Style.Triggers>
34+
</Style>
35+
</Border.Style>
36+
37+
<Grid>
38+
<Image Source="{Binding ImageSource}" Stretch="UniformToFill"/>
39+
40+
<CheckBox IsChecked="{Binding IsSelected}"
41+
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="2"/>
42+
</Grid>
43+
</Border>
44+
</DataTemplate>
45+
</ListBox.ItemTemplate>
46+
47+
<ListBox.ItemContainerStyle>
48+
<Style TargetType="ListBoxItem">
49+
<Setter Property="Padding" Value="0"/>
50+
<Setter Property="Margin" Value="0"/>
51+
<Setter Property="BorderThickness" Value="0"/>
52+
<Setter Property="Background" Value="Transparent"/>
53+
<Setter Property="Template">
54+
<Setter.Value>
55+
<ControlTemplate TargetType="ListBoxItem">
56+
<ContentPresenter />
57+
</ControlTemplate>
58+
</Setter.Value>
59+
</Setter>
60+
</Style>
61+
</ListBox.ItemContainerStyle>
62+
</ListBox>
63+
</Grid>
64+
</UserControl>

CommonControls/Thumbnails.xaml.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System.Collections.Generic;
2+
using System.Collections.ObjectModel;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using System.Windows.Controls;
9+
using System.Windows.Media.Imaging;
10+
11+
namespace CommonControls
12+
{
13+
public partial class Thumbnails : UserControl
14+
{
15+
// ObservableCollection automatically notifies the UI when items are added/removed
16+
public ObservableCollection<ThumbnailItem> ThumbCollection { get; set; } = new();
17+
18+
public static readonly DependencyProperty ThumbCellSizeProperty =
19+
DependencyProperty.Register(nameof(ThumbCellSize), typeof(double), typeof(Thumbnails), new PropertyMetadata(100.0));
20+
21+
public double ThumbCellSize
22+
{
23+
get => (double)GetValue(ThumbCellSizeProperty);
24+
set => SetValue(ThumbCellSizeProperty, value);
25+
}
26+
27+
public Thumbnails()
28+
{
29+
InitializeComponent();
30+
// Set the DataContext to this control so XAML can bind to ThumbCollection
31+
DataContext = this;
32+
}
33+
34+
public async Task LoadImages(Dictionary<int, string> sourceList)
35+
{
36+
ThumbCollection.Clear();
37+
38+
// Using your optimized loading logic, but slightly cleaner
39+
using (var semaphore = new SemaphoreSlim(4))
40+
{
41+
var tasks = sourceList.Select(async kvp =>
42+
{
43+
await semaphore.WaitAsync();
44+
try
45+
{
46+
var img = await LoadImageAsync(kvp.Value);
47+
if (img != null)
48+
{
49+
// Create the Data Item
50+
var item = new ThumbnailItem
51+
{
52+
Id = kvp.Key,
53+
FilePath = kvp.Value,
54+
ImageSource = img
55+
};
56+
57+
// Thread-safe add to collection
58+
Application.Current.Dispatcher.Invoke(() => ThumbCollection.Add(item));
59+
}
60+
}
61+
finally
62+
{
63+
semaphore.Release();
64+
}
65+
});
66+
67+
await Task.WhenAll(tasks);
68+
}
69+
}
70+
71+
private async Task<BitmapSource> LoadImageAsync(string path)
72+
{
73+
return await Task.Run(() =>
74+
{
75+
try
76+
{
77+
if (!File.Exists(path)) return null;
78+
var bi = new BitmapImage();
79+
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
80+
{
81+
bi.BeginInit();
82+
bi.StreamSource = fs;
83+
bi.CacheOption = BitmapCacheOption.OnLoad;
84+
bi.DecodePixelWidth = (int)ThumbCellSize; // Decode small to save RAM
85+
bi.EndInit();
86+
bi.Freeze(); // Vital for cross-thread
87+
}
88+
return bi;
89+
}
90+
catch { return null; }
91+
});
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)