Basic Library for WPF and Silverlight | ComponentOne
WPF and Silverlight Edition Basic Library / FilePicker / FilePicker Quick Start / Step 2 of 3: Adding Code to the Application
In This Topic
    Step 2 of 3: Adding Code to the Application
    In This Topic

    In the last step you set up a WPF or Silverlight application, but it currently does not function. 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 MainPage.xaml file or the 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:
    Visual Basic
    Copy Code
    Imports System.Windows.Media.Imaging
    Imports C1.WPF
    OR
    Imports System.Windows.Media.Imaging
    Imports C1.Silverlight
    

    C#
    Copy Code
    using System.Windows.Media.Imaging;
    using C1.WPF;
    OR
    using System.Windows.Media.Imaging;
    using C1.Silverlight;
    
    1. Add the following event handler to the MainPage.xaml.cs (or .vb) file or the MainWindow.xaml.cs (or .vb) file, below all the other methods in the MainPage class:
    Visual Basic
    Copy Code
    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.SetSource(stream)
            Dim image = New Image()
            image.Source = source
            image.Stretch = Stretch.Uniform
            image.Height = 100
            ListBox.Items.Add(image)
        End If
    End Sub
    

    C#
    Copy Code
    private void C1FilePicker_SelectedFilesChanged(object sender, EventArgs e)
    {
        if (C1FilePicker1.SelectedFile != null)
        {
            var stream = C1FilePicker1.SelectedFile.OpenRead();
            var source = new BitmapImage();
            source.SetSource(stream);
            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.

    1. Add the following code to handle the Click events of the Button controls on the page:
    Visual Basic
    Copy Code
    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#
    Copy Code
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        C1FilePicker1.ClearSelection();
    }
    private void button2_Click(object sender, RoutedEventArgs e)
    {
        ListBox.Items.Clear();
    }