using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel; 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 SPC.Kiosk.Base; using SPC.Kiosk.Common; namespace SPC.Kiosk.Popup { /// /// MiniBasketItem.xaml에 대한 상호 작용 논리 /// public partial class OptionSelector : UserControl { #region [ Members ] /// /// ItemData /// public static readonly DependencyProperty OptionDataProperty = DependencyProperty.Register(nameof(OptionData), typeof(List), typeof(OptionSelector), new PropertyMetadata(null, new PropertyChangedCallback(OnOptionDataPropertyChanged))); /// /// Display Language /// public static readonly DependencyProperty DisplayLanguagePropery = DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(OptionSelector), new PropertyMetadata(SupportLanguageType.ko, new PropertyChangedCallback(OnDisplayLanguageProperyChanged))); /// /// Vertical Scroll Offset /// public static readonly DependencyProperty VerticalOffsetProperty = DependencyProperty.Register(nameof(VerticalOffset), typeof(double), typeof(OptionSelector), new PropertyMetadata(0d, new PropertyChangedCallback(OnVerticalOffsetPropertyChanged))); /// /// EditMode /// public static readonly DependencyProperty EditModeProperty = DependencyProperty.Register(nameof(EditMode), typeof(bool), typeof(OptionSelector), new PropertyMetadata(false)); /// /// Command /// public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(OptionSelector), new UIPropertyMetadata(null)); /// /// Command /// public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } /// /// Vertical Scroll Offset /// public double VerticalOffset { get { return (double)GetValue(VerticalOffsetProperty); } set { SetValue(VerticalOffsetProperty, value); } } /// /// ItemData /// public List OptionData { get { return (List)GetValue(OptionDataProperty); } set { SetValue(OptionDataProperty, value); } } /// /// Display Language /// public SupportLanguageType DisplayLanguage { get { return (SupportLanguageType)GetValue(DisplayLanguagePropery); } set { SetValue(DisplayLanguagePropery, value); } } /// /// Edit Mode /// public bool EditMode { get { return (bool)GetValue(EditModeProperty); } set { SetValue(EditModeProperty, value); } } #region [ RoutedEvent 'SelectChange' ] /// /// MouseClicked Event /// public static readonly RoutedEvent SelectChangeEvent = EventManager.RegisterRoutedEvent(nameof(SelectChange), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(OptionSelector)); /// /// MouseClicked Routed Event Handler /// public event RoutedEventHandler SelectChange { add { AddHandler(SelectChangeEvent, value); } remove { RemoveHandler(SelectChangeEvent, value); } } #endregion RoutedEvent 'SelectChange' private double mouseDownYposition = -1; private bool MoseDown = false; #endregion Members #region [ Ctor & Etc ] /// /// Ctor /// public OptionSelector() { InitializeComponent(); OptionGrid.PreviewMouseDown += OptionGrid_PreviewMouseDown; OptionGrid.PreviewMouseUp += OptionGrid_PreviewMouseUp; OptionGrid.PreviewMouseMove += OptionGrid_PreviewMouseMove; OptionGrid.MouseLeave += OptionGrid_MouseLeave; } #endregion Ctor & Etc #region [ Methods ] /// /// Raise MouseClicked Event /// private void RaiseSelectChangeEvent(object _sender) { if (Command != null && Command.CanExecute(_sender)) { Command.Execute(_sender); } RoutedEventArgs newEventArgs = new RoutedEventArgs(OptionSelector.SelectChangeEvent, _sender); RaiseEvent(newEventArgs); } private void SetOptionStack(List newValue) { foreach (var aItem in OptionStack.Children) { if (aItem is OptionGroup aOptionGroup) { aOptionGroup.SelectChange -= OptionGroup_SelectChange; } } OptionStack.Children.Clear(); foreach (var aOptionGroup in newValue) { var newOptionGroup = new OptionGroup { EditMode = this.EditMode, GroupData = aOptionGroup, DisplayLanguage = this.DisplayLanguage }; newOptionGroup.SelectChange += OptionGroup_SelectChange; newOptionGroup.Name = string.Format("OptionGroup{0}", aOptionGroup.Index); this.ReregisterName(newOptionGroup.Name, newOptionGroup); this.OptionStack.Children.Add(newOptionGroup); } } #endregion Methods #region [ Event Handlers ] private void OptionGroup_SelectChange(object sender, RoutedEventArgs e) { RaiseSelectChangeEvent(OptionData); } private static void OnVerticalOffsetPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue != e.OldValue) { var target = d as OptionSelector; target.MainScroll.ScrollToVerticalOffset((double)e.NewValue); } } private static void OnOptionDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue is List newValue) { var target = d as OptionSelector; target.SetOptionStack(newValue); } } private static void OnDisplayLanguageProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (e.NewValue is SupportLanguageType newValue) { var target = d as OptionSelector; foreach (var aGroup in target.OptionStack.Children) { if (aGroup is OptionGroup optionGroup) { optionGroup.DisplayLanguage = newValue; } } } } private void OptionGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e) { var requstGrid = sender as Grid; mouseDownYposition = e.GetPosition(sender as Grid).Y; MoseDown = true; } private void OptionGrid_PreviewMouseUp(object sender, MouseButtonEventArgs e) { MoseDown = false; } private void OptionGrid_PreviewMouseMove(object sender, MouseEventArgs e) { if (!MoseDown) return; var requstGrid = sender as Grid; var curMouseYposition = e.GetPosition(requstGrid).Y; VerticalOffset = VerticalOffset + (mouseDownYposition - curMouseYposition) * 2; mouseDownYposition = curMouseYposition; } private void OptionGrid_MouseLeave(object sender, MouseEventArgs e) { MoseDown = false; } #endregion Event Handlers } }