using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Wpf_AiSportsMicrospace { /// /// Home.xaml 的交互逻辑 /// public partial class Home : Window { // 所有图片 public ObservableCollection AllImages { get; set; } = new ObservableCollection(); public ObservableCollection VisibleImages { get; set; } = new ObservableCollection(); private int selectedIndex = 0; public Home() { InitializeComponent(); // 示例图片路径 string projectRoot = System.IO.Path.Combine(AppContext.BaseDirectory, @"..\..\.."); string albumPath = System.IO.Path.Combine(projectRoot, "Resources", "Img", "Album"); string badgePath = System.IO.Path.Combine(projectRoot, "Resources", "Img", "Badge"); AllImages.Add(new CoverImage(System.IO.Path.Combine(albumPath, "1.jpg"), System.IO.Path.Combine(badgePath, "1.jpg"))); AllImages.Add(new CoverImage(System.IO.Path.Combine(albumPath, "2.jpg"), System.IO.Path.Combine(badgePath, "2.jpg"))); AllImages.Add(new CoverImage(System.IO.Path.Combine(albumPath, "3.jpg"), System.IO.Path.Combine(badgePath, "3.jpg"))); AllImages.Add(new CoverImage(System.IO.Path.Combine(albumPath, "4.jpg"), System.IO.Path.Combine(badgePath, "4.jpg"))); AllImages.Add(new CoverImage(System.IO.Path.Combine(albumPath, "5.jpg"), System.IO.Path.Combine(badgePath, "5.jpg"))); VisibleImagesControl.ItemsSource = VisibleImages; // 默认选中中间图片 selectedIndex = 2; UpdateVisibleImages(); } private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (sender is Border border && border.DataContext is CoverImage img) { int index = AllImages.IndexOf(img); if (index >= 0) { selectedIndex = index; UpdateVisibleImages(); } } } private void UpdateVisibleImages() { VisibleImages.Clear(); for (int offset = -1; offset <= 1; offset++) { int idx = selectedIndex + offset; if (idx >= 0 && idx < AllImages.Count) { var img = AllImages[idx]; img.IsSelected = offset == 0; VisibleImages.Add(img); } } } } public class CoverImage : INotifyPropertyChanged { public string Path { get; set; } // 主图路径 public string BadgePath { get; set; } // 小徽章路径 private bool _isSelected; public bool IsSelected { get => _isSelected; set { _isSelected = value; OnPropertyChanged(nameof(IsSelected)); } } public CoverImage(string path, string badgePath = null) { Path = path; BadgePath = badgePath; } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } }