Some update....

Jun 23, 2012 at 4:11 PM

I don't know where to share my modifications ...

Mousewhell, Keyboard page up and down, and now the child button receive click event or execute command if movement is less then TolleranzaPixelPerClick = 30 pixel.

here is my code :

    public class SliderPanel : PanelINotifyPropertyChanged
    {
 
        private int TolleranzaPixelPerClick = 30;
 
        public event PropertyChangedEventHandler PropertyChanged;
        protected void SendPropertyChanged(String propertyName)
        {
            if ((PropertyChanged != null))
            {
                PropertyChanged(thisnew PropertyChangedEventArgs(propertyName));
            }
        }
 
        private Point MouseStart, MouseNow, MouseFirst, MouseFinal;
 
        int _Counter;
        public int Counter
        {
            get
            {
                return _Counter;
            }
            set
            {
                _Counter = value;
                SendPropertyChanged("Counter");
            }
        }
 
        public SliderPanel()
        {
            // this.PreviewMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(SliderPanel_MouseLeftButtonDown);
            //this.PreviewMouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(SliderPanel_MouseLeftButtonUp);
            this.PreviewMouseWheel += new System.Windows.Input.MouseWheelEventHandler(SliderPanel_MouseWheel);
        }
 
        void SliderPanel_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            if (e.Delta > 0 && SelectedIndex > 0) SelectedIndex--;
            if (e.Delta < 0 && SelectedIndex < Children.Count - 1) SelectedIndex++;
        }
 
        private bool CaptureMouseMove = false;
 
        protected override void OnPreviewMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            // this.CaptureMouse();
 
            this.MouseStart = e.GetPosition(this);
            this.MouseNow = this.MouseStart;
            this.MouseFirst = this.MouseStart;
 
            CaptureMouseMove = true;
 
            // base.OnPreviewMouseLeftButtonDown(e);
        }
 
        protected override void OnPreviewMouseMove(System.Windows.Input.MouseEventArgs e)
        {
            if (CaptureMouseMove)
            {
                this.MouseNow = e.GetPosition(this);
 
                if (Math.Abs(this.MouseFirst.X - this.MouseNow.X) > TolleranzaPixelPerClick)
                {
                    this.CaptureMouse();
                }
 
                for (int i = 0; i < Children.Count; i++)
                {
                    TranslateTransform yu = new TranslateTransform(Children[i].RenderTransform.Value.OffsetX + (MouseNow.X - MouseStart.X), 0);
                    Children[i].RenderTransform = yu;
                }
 
                MouseStart = MouseNow;
            }
            else
            {
                base.OnPreviewMouseMove(e);
            }
        }
 
        protected override void OnPreviewMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                CaptureMouseMove = false;
                // this.MouseMove -= new System.Windows.Input.MouseEventHandler(SliderPanel_MouseMove);
                this.ReleaseMouseCapture();
 
                this.MouseFinal = e.GetPosition(this);
 
 
 
               
                    
                    if ((MouseFinal.X - MouseFirst.X) > 0)
                    {
                        if (Math.Abs(MouseFinal.X - MouseFirst.X) > 50)
                            Counter = Counter + 1;
                    }
                    else
                    {
                        if (Math.Abs(MouseFinal.X - MouseFirst.X) > 50)
                            Counter = Counter - 1;
                    }
 
                    double pTo, pFrom;
                    pTo = Counter * this.DesiredSize.Width;
                    pFrom = (MouseFinal.X - MouseFirst.X) > 0 ? (pTo - this.DesiredSize.Width) + (MouseFinal.X - MouseFirst.X) : (pTo + this.DesiredSize.Width) + (MouseFinal.X - MouseFirst.X);
 
                    if (Math.Abs(MouseFinal.X - MouseFirst.X) < 50)
                        pFrom = pTo + (MouseFinal.X - MouseFirst.X);
 
                    if (Counter > 0)
                    {
                        pTo = (Counter - 1) * this.DesiredSize.Width;
                        Counter = Counter - 1;
                    }
                    else if (Counter <= Children.Count * -1)
                    {
                        pTo = (Counter + 1) * this.DesiredSize.Width;
                        Counter = Counter + 1;
                    }
 
                    for (int i = 0; i < Children.Count; i++)
                    {
                        DoubleAnimation da = new DoubleAnimation(pFrom, pTo, new Duration(TimeSpan.FromSeconds(0.3)));
 
                        ((TranslateTransform)Children[i].RenderTransform).BeginAnimation(TranslateTransform.XProperty, da);
                    }
 
                    SelectedIndex = Math.Abs(Counter);
 
                    if (Math.Abs(MouseFinal.X - MouseFirst.X) < TolleranzaPixelPerClick)
                    {
                        base.OnPreviewMouseLeftButtonUp(e);
                    }
            }
            catch { }
 
        }
 
        protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            switch (e.Key)
            {
                case System.Windows.Input.Key.PageDown:
                    if (SelectedIndex < Children.Count - 1) SelectedIndex++;
                    break;
 
                case System.Windows.Input.Key.PageUp:
                    if (SelectedIndex > 0) SelectedIndex--;
                    break;
 
                default:
                    base.OnPreviewKeyDown(e);
                    break;
            }
            
        }
 
              int _SelectedIndex;
        public int SelectedIndex
        {
            get
            {
                return _SelectedIndex;
            }
            set
            {
                _SelectedIndex = value;
                SendPropertyChanged("SelectedIndex");
                AnimateBySelectedIndex(value);
            }
        }
 
        void AnimateBySelectedIndex(int index)
        {
            lock (this)
            {
 
 
                if (index < 0 || index > this.Children.Count - 1 || index * -1 == Counter)
                    return;
 
                index = Math.Abs(index)  * - 1;
                double pTo, pFrom;
                pTo = index * this.DesiredSize.Width;
                pFrom = index > Counter ? (pTo - this.DesiredSize.Width) : (pTo + this.DesiredSize.Width);
 
                Counter = index;
 
                for (int i = 0; i < Children.Count; i++)
                {
                    DoubleAnimation da = new DoubleAnimation(pFrom, pTo, new Duration(TimeSpan.FromSeconds(0.3)));
 
                    TranslateTransform yu = new TranslateTransform(Children[i].RenderTransform.Value.OffsetX, 0);
                    Children[i].RenderTransform = yu;
 
                    ((TranslateTransform)Children[i].RenderTransform).BeginAnimation(TranslateTransform.XProperty, da);
                }
            }
 
        }
 
 
        protected override System.Windows.Size MeasureOverride(System.Windows.Size availableSize)
        {
            Size resultSize = new Size(0, 0);
 
            foreach (UIElement child in this.Children)
            {
                child.Measure(availableSize);
                resultSize.Width = Math.Max(resultSize.Width, this.DesiredSize.Width);
                resultSize.Height = Math.Max(resultSize.Height, child.DesiredSize.Height);
            }
 
            resultSize.Width =
                double.IsPositiveInfinity(availableSize.Width) ?
                resultSize.Width : availableSize.Width;
 
            resultSize.Height =
                double.IsPositiveInfinity(availableSize.Height) ?
                resultSize.Height : availableSize.Height;
 
            return resultSize;
        }
 
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize)
        {
            for (int i = 0; i < Children.Count; i++)
            {
                Children[i].Arrange(new Rect((i * finalSize.Width), (double)0, finalSize.Width, finalSize.Height));
            }
 
            this.Clip = new RectangleGeometry(new Rect(0, 0, this.DesiredSize.Width, this.DesiredSize.Height));
 
            return base.ArrangeOverride(finalSize);
        }
    }
Jul 16, 2012 at 12:45 PM

Could you perhaps provide a sample application with these changes? I would like to see how they are implemented.