aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Touch/Controls/TouchStaticListBox.cs
blob: 023395006ba5167c1bcd4e5b49eb4e2b6204e5b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections;
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.Commands;
using Tango.Core.EventArguments;

namespace Tango.Touch.Controls
{
    public class TouchStaticListBox : Control
    {
        public IList ItemsSource
        {
            get { return (IList)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IList), typeof(TouchStaticListBox), new PropertyMetadata(null));

        public DataTemplate ItemTemplate
        {
            get { return (DataTemplate)GetValue(ItemTemplateProperty); }
            set { SetValue(ItemTemplateProperty, value); }
        }
        public static readonly DependencyProperty ItemTemplateProperty =
            DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(TouchStaticListBox), new PropertyMetadata(null));

        public Style ItemContainerStyle
        {
            get { return (Style)GetValue(ItemContainerStyleProperty); }
            set { SetValue(ItemContainerStyleProperty, value); }
        }
        public static readonly DependencyProperty ItemContainerStyleProperty =
            DependencyProperty.Register("ItemContainerStyle", typeof(Style), typeof(TouchStaticListBox), new PropertyMetadata(null));

        public Object SelectedItem
        {
            get { return (Object)GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register("SelectedItem", typeof(Object), typeof(TouchStaticListBox), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, (d, e) => (d as TouchStaticListBox).OnSelectdItemChanged()));

        public RelayCommand<TouchStaticListBoxItem> ListBoxItemLoadedCommand
        {
            get { return (RelayCommand<TouchStaticListBoxItem>)GetValue(ListBoxItemLoadedCommandProperty); }
            set { SetValue(ListBoxItemLoadedCommandProperty, value); }
        }
        public static readonly DependencyProperty ListBoxItemLoadedCommandProperty =
            DependencyProperty.Register("ListBoxItemLoadedCommand", typeof(RelayCommand<TouchStaticListBoxItem>), typeof(TouchStaticListBox), new PropertyMetadata(null));

        public ItemsPanelTemplate ItemsPanel
        {
            get { return (ItemsPanelTemplate)GetValue(ItemsPanelProperty); }
            set { SetValue(ItemsPanelProperty, value); }
        }
        public static readonly DependencyProperty ItemsPanelProperty =
            DependencyProperty.Register("ItemsPanel", typeof(ItemsPanelTemplate), typeof(TouchStaticListBox), new PropertyMetadata(null));



        static TouchStaticListBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TouchStaticListBox), new FrameworkPropertyMetadata(typeof(TouchStaticListBox)));
        }

        public TouchStaticListBox()
        {
            ListBoxItemLoadedCommand = new RelayCommand<TouchStaticListBoxItem>(RegisterListBoxItemEvents);
            Loaded += TouchStaticListBox_Loaded;
        }

        private void TouchStaticListBox_Loaded(object sender, RoutedEventArgs e)
        {
            OnSelectdItemChanged();
        }

        private void RegisterListBoxItemEvents(TouchStaticListBoxItem item)
        {
            if (item.Tag == null)
            {
                item.Tag = 1;
                item.RegisterForPreviewMouseOrTouchUp(OnItemMouseUp);
            }
        }

        private void OnItemMouseUp(object sender, MouseOrTouchEventArgs e)
        {
            var scrollViewer = this.FindAncestor<LightTouchScrollViewer>();
            if (scrollViewer != null && scrollViewer.IsAfterScrolling) return;

            var item = (e.Source is TouchStaticListBoxItem) ? e.Source as TouchStaticListBoxItem : (e.Source as DependencyObject).FindAncestor<TouchStaticListBoxItem>();
            SelectedItem = item.DataContext;
        }

        private void OnSelectdItemChanged()
        {
            var items = GetItems();
            items.ForEach(x => x.IsSelected = false);

            if (SelectedItem != null)
            {
                var selected_item = items.FirstOrDefault(x => SelectedItem.Equals(x.DataContext));

                if (selected_item != null)
                {
                    selected_item.IsSelected = true;
                }
            }
        }

        private List<TouchStaticListBoxItem> GetItems()
        {
            return this.FindVisualChildren<TouchStaticListBoxItem>().ToList();
        }
    }
}