Click here to Skip to main content
15,878,748 members
Articles / Desktop Programming / WPF

Multi-touch development with WPF - A multi-touch RSS reader

Rate me:
Please Sign up or sign in to vote.
4.94/5 (36 votes)
1 Nov 2009GPL315 min read 177.2K   5.7K   97  
A multi-touch RSS reader built with Multi-Touch Vista.
using System;
using System.Windows;
using Multitouch.Framework.WPF.Controls;
using VirtualDreams.TouchReader.Factory;

namespace VirtualDreams.TouchReader.Behavior
{
    public class ElementDraggedCreateViewBehavior
    {
        #region PanelViewFactory

        /// <summary>
        /// PanelViewFactory Attached Dependency Property
        /// </summary>
        public static readonly DependencyProperty PanelViewFactoryProperty =
            DependencyProperty.RegisterAttached("PanelViewFactory", typeof(IPanelViewFactory), typeof(ElementDraggedCreateViewBehavior),
                                                new FrameworkPropertyMetadata(null, OnPanelViewFactoryChanged));

        /// <summary>
        /// Gets the PanelViewFactory property.  This dependency property 
        /// indicates which factory will be used to create the views.
        /// </summary>
        public static IPanelViewFactory GetPanelViewFactory(DependencyObject d)
        {
            if (d == null)
                throw new ArgumentNullException("d");
            return (IPanelViewFactory)d.GetValue(PanelViewFactoryProperty);
        }

        /// <summary>
        /// Sets the PanelViewFactory property.  This dependency property 
        /// indicates which factory will be used to create the views.
        /// </summary>
        public static void SetPanelViewFactory(DependencyObject d, IPanelViewFactory value)
        {
            if (d == null)
                throw new ArgumentNullException("d");
            d.SetValue(PanelViewFactoryProperty, value);
        }

        /// <summary>
        /// Handles changes to the PanelViewFactory property.
        /// </summary>
        private static void OnPanelViewFactoryChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var scrollViewer = d as DraggableScrollViewer;
            if (scrollViewer != null)
            {
                if (e.NewValue != null)
                    scrollViewer.ElementDragged += ControlElementDragged;
                else
                    scrollViewer.ElementDragged -= ControlElementDragged;
            }
        }

        #endregion

        static void ControlElementDragged(object sender, ElementRoutedEventArgs e)
        {
            IPanelViewFactory viewFactory = GetPanelViewFactory((DependencyObject)sender);
            if (viewFactory == null) return;
            var position = e.GetPosition(viewFactory.Panel);
            var offset = e.Offset;
            viewFactory.CreateViewFromDataContext(e.Element.DataContext, new Point(position.X - offset.X, position.Y - offset.Y));
        }
       
    }
}

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
Virtual Dreams
Brazil Brazil
Hi! I'm Roberto. I'm a Brazilian Engineering student at the University of São Paulo and the Ecole Centrale de Lille (France).

I've participated in the Imagine Cup competition and went to the world finals every year from 2005 to 2009. I also won the 1st place award in 2006, in India, for the Interface Design invitational, in 2007 in Korea, for the Embedded Development invitational, and in 2009 in Egypt for the Windows Mobile Award.

Currently I keep a blog (in English and Portuguese) at http://virtualdreams.com.br/blog/ and a weekly webcast about WPF and Silverlight (in Portuguese) at http://www.xamlcast.net.

Comments and Discussions