WPF: Add separators in WPF thumbnail viewer.

Code samples for VintaSoft Imaging .NET SDK. Here you can request a code sample.

Moderator: Alex

Post Reply
Alex
Site Admin
Posts: 1461
Joined: Thu Jul 10, 2008 2:21 pm

WPF: Add separators in WPF thumbnail viewer.

Post by Alex » Fri Apr 29, 2016 11:51 am

This topic contains code sample that shows how to add separators between thumbnails in WPF thumbnail viewer:

Code: Select all

...
thumbnailViewer.SizeChanged += new SizeChangedEventHandler(thumbnailViewer_SizeChanged);
AddSeparator(thumbnailViewer, 1);
AddSeparator(thumbnailViewer, 3);
...


/// <summary>
/// Handles the SizeChanged event of the WpfThumbnailViewer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="SizeChangedEventArgs"/> instance containing the event data.</param>
private void thumbnailViewer_SizeChanged(object sender, SizeChangedEventArgs e)
{
    UpdateSeparatorPositions((WpfThumbnailViewer)sender);
}

/// <summary>
/// Adds the separator to the specified thumbnail viewer.
/// </summary>
/// <param name="thumbnailViewer">The thumbnail viewer.</param>
/// <param name="index">The image index.</param>
private void AddSeparator(WpfThumbnailViewer thumbnailViewer, int index)
{
    Grid thumbnailViewerGrid = (Grid)thumbnailViewer.Content;
    Canvas thumbnailViewerCanvas = (Canvas)thumbnailViewerGrid.Children[1];

    Size thumbnailSize = thumbnailViewer.ThumbnailSize;

    System.Windows.Shapes.Rectangle separator = new System.Windows.Shapes.Rectangle();
    separator.Tag = index;
    separator.Width = 2;
    separator.Height = thumbnailSize.Width;
    separator.Fill = Brushes.Red;

    thumbnailViewerCanvas.Children.Add(separator);

    separator.Visibility = Visibility.Visible;

    UpdateSeparatorPositions(thumbnailViewer);
}

/// <summary>
/// Updates the separator positions.
/// </summary>
/// <param name="thumbnailViewer">The thumbnail viewer.</param>
private void UpdateSeparatorPositions(WpfThumbnailViewer thumbnailViewer)
{
    Grid thumbnailViewerGrid = (Grid)thumbnailViewer.Content;
    Canvas thumbnailViewerCanvas = (Canvas)thumbnailViewerGrid.Children[1];

    foreach (System.Windows.Shapes.Rectangle separator in thumbnailViewerCanvas.Children)
    {
        if (separator.Tag != null)
        {
            int index = (int)separator.Tag;

            if (index >= thumbnailViewer.Images.Count)
            {
                separator.Visibility = Visibility.Hidden;
            }
            else
            {
                Size thumbnailSize = new Size(
                    thumbnailViewer.Thumbnails[index].ActualWidth,
                    thumbnailViewer.Thumbnails[index].ActualHeight);

                separator.Height = thumbnailSize.Width;

                int cols, rows;
                thumbnailViewer.GetThumbnailsGridSize(out cols, out rows);

                int row = index / cols;
                int col = index % cols;

                Point separatorPosition = new Point();
                separatorPosition.X = col * thumbnailSize.Width - separator.Width / 2;
                separatorPosition.Y = row * thumbnailSize.Height;

                Canvas.SetLeft(separator, separatorPosition.X);
                Canvas.SetTop(separator, separatorPosition.Y);

                separator.Visibility = Visibility.Visible;
            }
        }
    }
}

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest