# TriggerManagement [C#]

     using System;
     using System.Collections.Generic;
     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;
     using WpfApplication1.ServiceReference1;
     using System.Collections.ObjectModel;
     using System.Globalization;

     namespace WpfApplication1
     {

         // Interaction logic for TriggerManagement.xaml
         public partial class TriggerManagement : UserControl
         {
             ServiceSample sample = new ServiceSample();

             public TriggerManagement()
             {
                 InitializeComponent();
             }

             //Initialize the XAML object and retrieve group from server by login
             public TriggerManagement(string login, string password)
             {
                 InitializeComponent();

                 //Login and retrive group
                 List<View_Group> listResult;
                 sample.GetGroup(out listResult, login, password);

                 //Bind group to TreeList
                 treeGroup.ItemsSource = TreeNode.GetTreeNode(listResult);
             }

             //Select group and retrive coresponding media trigger
             private void treeGroup_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
             {
                 //Get Medias for selected group
                 List<View_Media> listResult;
                 sample.GetTrigger(out listResult, (treeGroup.SelectedItem as TreeNode).Id ?? 0);

                 //Update media thumbnail path
                 listResult.ForEach(x => x.ThumbnailPath = (x.ThumbnailPath != null) ? (GlobalParam.MEDIA_URL + x.ThumbnailPath) : "");

                 //Bind group to List
                 gridTrigger.ItemsSource = new ObservableCollection<View_Media>(listResult);
             }

             //Select media
             private void gridTrigger_SelectionChanged(object sender, SelectionChangedEventArgs e)
             {
                 if (gridTrigger.SelectedItem == null) return;

                 //Display if the media as 'Published' or 'Unpublished'
                 btnPublish.Content = (gridTrigger.SelectedItem as View_Media).FolderId == -3 ? "Unpublished" : "Published";
             }

             //Published / Unpublished a media
             private void btnPublish_Click(object sender, RoutedEventArgs e)
             {
                 //keep the media to publish
                 View_Media trigger = gridTrigger.SelectedItem as View_Media;

                 //change this folder id
                 trigger.FolderId = trigger.FolderId == -3 ? -4 : -3;

                 //Update the media
                 sample.UpdateMedia(ref trigger);

                 //Refresh the media collection
                 gridTrigger_SelectionChanged(null, null);
             }
         }

         public class PublishConvert2 : IValueConverter
         {
             public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
             {
                 return System.Convert.ToInt32(value) == -3 ? "Published" : "Not Published";
             }

             public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
             {
                 throw new NotImplementedException();
             }
         }

     }