using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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 Tango.Core.ExtensionMethods; namespace Tango.Touch.Controls { public class TouchNativeListBox : ListBox { /// /// Gets or sets the ripple factor. /// public double RippleFactor { get { return (double)GetValue(RippleFactorProperty); } set { SetValue(RippleFactorProperty, value); } } public static readonly DependencyProperty RippleFactorProperty = DependencyProperty.Register("RippleFactor", typeof(double), typeof(TouchNativeListBox), new PropertyMetadata(30.0)); /// /// Gets or sets a value indicating whether to prevent selection on mouse down. /// public bool PreventTouchDownSelect { get { return (bool)GetValue(PreventTouchDownSelectProperty); } set { SetValue(PreventTouchDownSelectProperty, value); } } public static readonly DependencyProperty PreventTouchDownSelectProperty = DependencyProperty.Register("PreventTouchDownSelect", typeof(bool), typeof(TouchNativeListBox), new PropertyMetadata(true)); /// /// Gets or sets the scroll viewer. /// public LightTouchScrollViewer ScrollViewer { get { return (LightTouchScrollViewer)GetValue(ScrollViewerProperty); } set { SetValue(ScrollViewerProperty, value); } } public static readonly DependencyProperty ScrollViewerProperty = DependencyProperty.Register("ScrollViewer", typeof(LightTouchScrollViewer), typeof(TouchNativeListBox), new PropertyMetadata(null)); public override void OnApplyTemplate() { base.OnApplyTemplate(); ScrollViewer = GetTemplateChild("PART_ScrollViewer") as LightTouchScrollViewer; } protected override void OnMouseUp(MouseButtonEventArgs e) { if (ScrollViewer.IsAfterScrolling) return; if (e.ChangedButton == MouseButton.Left) { DependencyObject obj = this.ContainerFromElement((Visual)e.OriginalSource); if (obj != null) { FrameworkElement element = obj as FrameworkElement; if (element != null) { if (String.IsNullOrWhiteSpace(SelectedValuePath)) { ListBoxItem item = element as ListBoxItem; if (item != null) { this.SelectedItem = item.DataContext; } } else { object context = element.DataContext; if (context != null) { SelectedValue = context.GetPropertyValueByPath(SelectedValuePath); } } } } } } protected override void OnPreviewMouseDown(MouseButtonEventArgs e) { if (PreventTouchDownSelect) { e.Handled = true; } } static TouchNativeListBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchNativeListBox), new FrameworkPropertyMetadata(typeof(TouchNativeListBox))); } } }