using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; 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; namespace SPC.Kiosk.Common { /// /// IntroView.xaml에 대한 상호 작용 논리 /// public partial class IntroView : UserControl { #region [ Members ] /// /// Intro Items /// public static readonly DependencyProperty IntroItemsProperty = DependencyProperty.Register(nameof(IntroItems), typeof(ObservableCollection), typeof(IntroView), new PropertyMetadata(null, new PropertyChangedCallback(OnIntroItemsPropertyChanged))); /// /// Click Command /// public static readonly DependencyProperty CommandProperty = DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(IntroView), new UIPropertyMetadata(null)); /// /// Intro ITems /// public ObservableCollection IntroItems { get { return (ObservableCollection)GetValue(IntroItemsProperty); } set { SetValue(IntroItemsProperty, value); } } /// /// Click Commad /// public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } #region [ RoutedEvent 'MouseClicked' ] /// /// MouseClicked Event /// public static readonly RoutedEvent MouseClickedEvent = EventManager.RegisterRoutedEvent(nameof(MouseClicked), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(IntroView)); /// /// MouseClicked Routed Event Handler /// public event RoutedEventHandler MouseClicked { add { AddHandler(MouseClickedEvent, value); } remove { RemoveHandler(MouseClickedEvent, value); } } #endregion RoutedEvent 'MouseClicked' #endregion Members #region [ Ctor ] /// /// Ctor /// public IntroView() { InitializeComponent(); } #endregion Ctor #region [ Methods ] private void RaiseMouseClickedEvent(object _sender) { //Command Excute if (Command != null && Command.CanExecute(_sender)) { Command.Execute(_sender); } RoutedEventArgs newEventArgs = new RoutedEventArgs(IntroView.MouseClickedEvent, _sender); RaiseEvent(newEventArgs); } private void AddStack(StackPanel _targetStack, M_IntroItems _stackContents) { try { if (_stackContents.StackContents is List stackContents) { foreach (var aContents in stackContents) { var newName = string.Format("MediaRoll{0}_{1}", _stackContents.ItemsStack, stackContents.IndexOf(aContents)); var newMediaRoll = new MediaRoll { Width = _stackContents.StackWidth, ListMedia = new ObservableCollection(aContents.MediaRollItems) }; newMediaRoll.MouseClicked += NewMediaRoll_MouseClicked; _targetStack.Children.Add(newMediaRoll); this.ReregisterName(newName, newMediaRoll); } } } catch (Exception ex) { CommonLog.ErrorLogWrite(this, "AddStack()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace)); } } private void RemoveStack(StackPanel _targetStack, M_IntroItems _stackContents) { try { if (_stackContents.StackContents is List stackContents) { foreach (var aContents in stackContents) { var oldName = string.Format("MediaRoll{0}_{1}", _stackContents.ItemsStack, stackContents.IndexOf(aContents)); var oldMediaRoll = this.FindName(oldName); if (oldMediaRoll is MediaRoll removeMedia) { removeMedia.MouseClicked -= NewMediaRoll_MouseClicked; } } _targetStack.Children.Clear(); } } catch (Exception ex) { CommonLog.ErrorLogWrite(this, "RemoveStack()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace)); } } private void NewMediaRoll_MouseClicked(object sender, RoutedEventArgs e) { if (e.OriginalSource is FrameworkElement getElement) { if (getElement.Tag is M_MediaRollItem selectItem) { RaiseMouseClickedEvent(selectItem); } } } #endregion Methods #region [ Event Handlers ] private static void OnIntroItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { try { var target = d as IntroView; if (e.NewValue is ObservableCollection introItems && introItems.Count > 0) { if (e.OldValue is ObservableCollection oldIntroItems) { if (introItems.Count.Equals(1)) { target.RemoveStack(target.IntroMain, introItems.FirstOrDefault()); } else if (introItems.Count.Equals(2)) { foreach (var aIntroItem in introItems) { var oldStack = target.FindName(string.Format("IntroStack{0}", introItems.IndexOf(aIntroItem))); if (oldStack is StackPanel removeStack) { target.RemoveStack(removeStack, aIntroItem); } } target.IntroMain.Children.Clear(); } } if (introItems.Count.Equals(1)) { target.IntroMain.Orientation = Orientation.Vertical; target.AddStack(target.IntroMain, introItems.FirstOrDefault()); } else if (introItems.Count.Equals(2)) { target.IntroMain.Orientation = Orientation.Horizontal; foreach (var aIntroItem in introItems) { var newStackPanel = new StackPanel { Orientation = Orientation.Vertical }; target.AddStack(newStackPanel, aIntroItem); target.IntroMain.Children.Add(newStackPanel); target.ReregisterName(string.Format("IntroStack{0}", introItems.IndexOf(aIntroItem)), newStackPanel); } } } else { if (e.OldValue is ObservableCollection oldIntroItems) { if (oldIntroItems.Count.Equals(1)) { target.RemoveStack(target.IntroMain, oldIntroItems.FirstOrDefault()); } else if (oldIntroItems.Count.Equals(2)) { foreach (var aIntroItem in oldIntroItems) { var oldStack = target.FindName(string.Format("IntroStack{0}", oldIntroItems.IndexOf(aIntroItem))); if (oldStack is StackPanel removeStack) { target.RemoveStack(removeStack, aIntroItem); } } target.IntroMain.Children.Clear(); } } } } catch (Exception ex) { CommonLog.ErrorLogWrite(d, "OnIntroItemsPropertyChanged()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace)); } } #endregion Event Handlers } }