WPF/C# 点击图片缩略图 -> 显示大图
WPF/C# Click on image thumbnail -> show that image big
我有来自相机的图像(但现在假设它们在磁盘上)。
我想要这些作为缩略图列表:
[thumb1] [thumb2] [thumb3] ... [thumb100]
单击缩略图时,我希望将该图像放在 UI(相同 window)的大图像视图中。有点像 this question.
我是 C# 菜鸟,无法按照该答案进行操作:(
此外,剧情转折:我还想在我的相机上安装一个 "live mode",这意味着 "big image" UI 元素也必须能够直接显示来自相机的图像实时("live mode" 期间无缩略图)。
这是我目前得到的代码。我还需要补充什么吗?
XML:
<StackPanel>
<!--This is the big image that I want to see when I click each thumbnail: -->
<Image Source="{Binding BigImage}"/>
<!--This is the thumbnail chain at the bottom: -->
<ListBox ItemsSource="{Binding Thumbnails}" Height="100">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding BigImage}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ListBox>
</StackPanel>
CS:
public partial class MainWindow : Window, INotifyPropertyChanged
{
// this is the big image on the UI
public ImageSource BigImage { get; set; }
// this is a collection of thumbnails. When clicked, that image should become
// the "big image"
public ObservableCollection<ImageSource> Thumbnails { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
Thumbnails = new ObservableCollection<ImageSource>();
Thumbnails.Add(BigImage);
Thumbnails.Add(new BitmapImage(new Uri(@"C:\Raw0.bmp")));
Thumbnails.Add(new BitmapImage(new Uri(@"C:\Raw1.bmp")));
Thumbnails.Add(new BitmapImage(new Uri(@"C:\Raw2.bmp")));
NotifyPropertyChanged("Thumbnails");
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
BigImage 不是 ImageSource 的 属性。 Image 控件的 Source 需要绑定到数据上下文本身。通过使用点来做到这一点。
<Image Source="{Binding .}"/>
然后将ItemsControl的SelectedItem绑定到BigImage对象上。
<ListBox ItemsSource="{Binding Thumbnails}" SelectedItem="{Binding BigImage}" Height="100">
我有来自相机的图像(但现在假设它们在磁盘上)。 我想要这些作为缩略图列表:
[thumb1] [thumb2] [thumb3] ... [thumb100]
单击缩略图时,我希望将该图像放在 UI(相同 window)的大图像视图中。有点像 this question.
我是 C# 菜鸟,无法按照该答案进行操作:(
此外,剧情转折:我还想在我的相机上安装一个 "live mode",这意味着 "big image" UI 元素也必须能够直接显示来自相机的图像实时("live mode" 期间无缩略图)。
这是我目前得到的代码。我还需要补充什么吗?
XML:
<StackPanel>
<!--This is the big image that I want to see when I click each thumbnail: -->
<Image Source="{Binding BigImage}"/>
<!--This is the thumbnail chain at the bottom: -->
<ListBox ItemsSource="{Binding Thumbnails}" Height="100">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding BigImage}"/>
</DataTemplate>
</ListBox.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ListBox>
</StackPanel>
CS:
public partial class MainWindow : Window, INotifyPropertyChanged
{
// this is the big image on the UI
public ImageSource BigImage { get; set; }
// this is a collection of thumbnails. When clicked, that image should become
// the "big image"
public ObservableCollection<ImageSource> Thumbnails { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
Thumbnails = new ObservableCollection<ImageSource>();
Thumbnails.Add(BigImage);
Thumbnails.Add(new BitmapImage(new Uri(@"C:\Raw0.bmp")));
Thumbnails.Add(new BitmapImage(new Uri(@"C:\Raw1.bmp")));
Thumbnails.Add(new BitmapImage(new Uri(@"C:\Raw2.bmp")));
NotifyPropertyChanged("Thumbnails");
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
BigImage 不是 ImageSource 的 属性。 Image 控件的 Source 需要绑定到数据上下文本身。通过使用点来做到这一点。
<Image Source="{Binding .}"/>
然后将ItemsControl的SelectedItem绑定到BigImage对象上。
<ListBox ItemsSource="{Binding Thumbnails}" SelectedItem="{Binding BigImage}" Height="100">