witam,zacząłem robić przeglądarkę zdjęć w wpf-ie i stanąłem pewnym momencie.nie działa mi metoda która wyświetli zaznaczone zdjęcie z listy miniatur w panelu głównym,wypluwa komunikat:
Object reference not set to an instance of an object.
Co zrobiłem nie tak?
<Window x:Class="przeg_test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="ImageDataTemplate">
<Image Source="{Binding zdjecie}" Width="125"
Height="125" />
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Name="lista" Grid.Row="1" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Stretch"
ItemsSource="{Binding}" ItemTemplate="{StaticResource ImageDataTemplate}" SelectionChanged="lista_SelectionChanged">
</ListBox>
<Button Click="Dodaj_zdjecie" Content="Dodaj zdjęcie" Height="23" Width="76" Grid.ColumnSpan="2"></Button>
<Grid Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch" Margin="5" Name="grid1" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<!-- PANEL GLOWNY DO WYSWIETLANIA ZDJEC -->
<Image Grid.Row="0" HorizontalAlignment="Center" Margin="5" Name="image" Stretch="UniformToFill" VerticalAlignment="Center"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Name="stackPanel1" Grid.Row="1">
<Button Content="Odwróć" Height="23" Name="odwroc" Width="76" />
<Button Content="Usuń" Height="23" Name="usun" Width="76" />
</StackPanel>
</Grid>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.IO;
using System.Windows.Media.Effects;
using System.Collections.ObjectModel;
namespace przeg_test
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
ObservableCollection<Zdjecia> a = new ObservableCollection<Zdjecia>
{
};
public MainWindow()
{
InitializeComponent();
lista.ItemsSource = a;
}
string wybrany_plik;
private void Dodaj_zdjecie(object sender, RoutedEventArgs e)
{
System.Windows.Forms.OpenFileDialog plik = new System.Windows.Forms.OpenFileDialog();
plik.InitialDirectory = "c:\\";
plik.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
plik.RestoreDirectory = true;
if (plik.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
wybrany_plik = plik.FileName;
a.Add(new Zdjecia { zdjecie = new BitmapImage(new Uri(wybrany_plik)) });
}
}
//wyswietla zaznaczone zdjecie z listy miniatur w panelu glownym
private void lista_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
BitmapImage img = (sender as ListBox).SelectedItem as BitmapImage;
image.Source = new BitmapImage(new Uri(img.ToString()));
}
}
class Zdjecia
{
public BitmapImage zdjecie
{
get;
set;
}
}
}