105 lines
3.7 KiB
C#
Raw Normal View History

2025-09-22 09:47:35 +08:00
using System;
using System.Collections.Generic;
2025-09-22 16:49:49 +08:00
using System.Collections.ObjectModel;
using System.ComponentModel;
2025-09-22 09:47:35 +08:00
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
{
/// <summary>
/// Home.xaml 的交互逻辑
/// </summary>
2025-09-22 16:49:49 +08:00
public partial class Home : Window
{
// 所有图片
public ObservableCollection<CoverImage> AllImages { get; set; } = new ObservableCollection<CoverImage>();
public ObservableCollection<CoverImage> VisibleImages { get; set; } = new ObservableCollection<CoverImage>();
private int selectedIndex = 0;
2025-09-22 09:47:35 +08:00
public Home()
{
InitializeComponent();
2025-09-22 16:49:49 +08:00
// 示例图片路径
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);
}
}
2025-09-22 09:47:35 +08:00
}
}
2025-09-22 16:49:49 +08:00
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));
}
2025-09-22 09:47:35 +08:00
}