Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / WPF

C.B.R.

Rate me:
Please Sign up or sign in to vote.
4.96/5 (52 votes)
22 Oct 2012GPL329 min read 124.3K   1.8K   132  
Comic and electronic publication reader with library management, extended file conversion, and devices support.
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 CBR.Core.Models;
using CBR.ViewModels;
using CBR.Core.Helpers;

namespace CBR.Views
{
    /// <summary>
    /// Interaction logic for ExplorerView.xaml
    /// </summary>
    public partial class ExplorerView : UserControl
    {
        #region --------------------CONSTRUCTOR--------------------

        private DragHelper _drager = null;

        public ExplorerView()
        {
            InitializeComponent();

            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                DataContext = new ExplorerViewModel();

                Messenger.Default.Register<MessageBase>(this, (s) => { Grouping(); });

                _drager = new DragHelper(CatalogListBox);
                _drager.OnStartDrag += new StartDragEventHandler(drag_OnStartDrag);
                _drager.OnContinueDrag += new StartDragEventHandler(_drager_OnContinueDrag);
            }
        }
        
        #endregion

        #region --------------------START DRAG--------------------

        void drag_OnStartDrag(object sender, MouseEventArgs e)
        {
            try
            {
                // Get the dragged ListViewItem
                ListBoxItem item = VisualHelper.FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource);

                if (item != null)
                {
                    Book bk = (Book)CatalogListBox.ItemContainerGenerator.ItemFromContainer(item);
                    _drager.DragDropContinue(bk != null);
                }
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("ExplorerView:drag_OnStartDrag", err);
            }
        }

        void _drager_OnContinueDrag(object sender, MouseEventArgs e)
        {
            try
            {
                // Get the dragged ListViewItem
                ListBoxItem item = VisualHelper.FindAnchestor<ListBoxItem>((DependencyObject)e.OriginalSource);

                if (item != null)
                {
                    Book bk = (Book)CatalogListBox.ItemContainerGenerator.ItemFromContainer(item);

                    // Find the data behind the item + Initialize the drag & drop operation
                    DataObject dragData = new DataObject("CBR.Book.Path", bk.FilePath);
                    _drager.DoDragDrop(dragData, item);
                }
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("ExplorerView:drag_OnStartDrag", err);
            }
        }

        #endregion

        #region --------------------INTERNAL--------------------

        /// <summary>
        /// handle a item double click (using Interaction.Triggers is not working because on list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Item_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                ExplorerViewModel model = DataContext as ExplorerViewModel;
                if (model == null)
                    return;

                model.ForwardCommand.Execute( "BookReadCommand" );
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("ExplorerView:Grouping", err);
            }
        } 

        /// <summary>
        /// called by the message register delegate
        /// </summary>
        public void Grouping()
        {
            try
            {
                ExplorerViewModel model = DataContext as ExplorerViewModel;
                if (model == null)
                    return;

                if (model.Books.GroupDescriptions.Count > 0)
                    CatalogListBox.GroupStyle.Add(new GroupStyle() { ContainerStyle = (Style)FindResource("AlphaGroupContainerStyle") });
                else
                {
                    CatalogListBox.GroupStyle.Clear();
                }
            }
            catch (Exception err)
            {
                ExceptionHelper.Manage("ExplorerView:Grouping", err);
            }
        }

        #endregion 
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions