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();
}
}
}