In the last step you set up a WPF application, but if you run your application the controls currently do nothing. In this step you'll continue by adding code to add functionality to the application.
Complete the following steps:
1. Navigate to the Solution Explorer, right-click MainWindow.xaml file, and select View Code to switch to Code view.
2. In Code view, add the following import statements to the top of the page if they are not included:
Imports System.Windows.Media.Imaging
Imports C1.WPF
•C#
using System.Windows.Media.Imaging;
using C1.WPF;
3. Add the following event handler to the MainWindow.xaml.cs (or .vb) file, below all the other methods in the MainPage class:
Private Sub C1FilePicker_SelectedFilesChanged(sender As Object, e As EventArgs)
If C1FilePicker1.SelectedFile IsNot Nothing Then
Dim stream = C1FilePicker1.SelectedFile.OpenRead()
Dim source = New BitmapImage()
source.BeginInit()
source.StreamSource = stream
source.EndInit()
Dim image = New Image()
image.Source = source
image.Stretch = Stretch.Uniform
image.Height = 100
ListBox.Items.Add(image)
End If
End Sub
•C#
private void C1FilePicker_SelectedFilesChanged(object sender, EventArgs e)
{
if (C1FilePicker1.SelectedFile != null)
{
var stream = C1FilePicker1.SelectedFile.OpenRead();
var source = new BitmapImage();
source.BeginInit();
source.StreamSource = stream;
source.EndInit();
var image = new Image();
image.Source = source;
image.Stretch = Stretch.Uniform;
image.Height = 100;
ListBox.Items.Add(image);
}
}
This code handles the SelectedFilesChanged event. When a user selects an image with the C1FilePicker control, the image is customized and added to the ListBox control.
4. Add the following code to handle the Click events of the Button controls on the page:
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
C1FilePicker1.ClearSelection()
End Sub
Private Sub button2_Click(sender As Object, e As RoutedEventArgs)
ListBox.Items.Clear()
End Sub
•C#
private void button1_Click(object sender, RoutedEventArgs e)
{
C1FilePicker1.ClearSelection();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
ListBox.Items.Clear();
}
What You've Accomplished
In this step you added code to add functionality to your application. In the next step you'll run your application and observe some of the run-time interactions possible with ComponentOne FilePicker for WPF.