Advertisement
Guest User

LstBox Selected Item with Checkbox Problem - C#

a guest
Dec 16th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using System.Windows.Threading;
  19. using MahApps.Metro.Controls;
  20.  
  21. namespace Outlook_Add_In_Test
  22. {
  23.     /// <summary>
  24.     /// Interaction logic for WPFDistroList.xaml
  25.     /// </summary>
  26.     public partial class WPFDistroList : MetroWindow
  27.     {
  28.         DispatcherTimer dispatcherTimer = new DispatcherTimer();
  29.         public static Dictionary<string, userInits> returnedResults { get; set; }
  30.         public bool selectionchanged { get; set; }
  31.         WPFNewEventv2 mainWindow;
  32.         private ObservableCollection<SelectableItem> Items { get; set; }
  33.  
  34.         public WPFDistroList(WPFNewEventv2 oldWindow)
  35.         {
  36.             InitializeComponent();
  37.  
  38.             selectionchanged = false;
  39.  
  40.             mainWindow = oldWindow;
  41.  
  42.             //Set the Tick Method
  43.             dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
  44.             //Set timer interval in seconds ( (0, 0, 1) = 1 second)
  45.             dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
  46.  
  47.             LoadingRing.IsActive = true;
  48.             BtnOk.Background = Brushes.DarkGray;
  49.             BtnOk.IsEnabled = false;
  50.             Thread th = new Thread(searchThread);
  51.             th.Start();
  52.         }
  53.  
  54.         private void searchThread()
  55.         {
  56.             //Connect to database and get a List of all the users Initials
  57.             Database connect = new Database();
  58.             returnedResults = connect.ExecGetUserInits();
  59.  
  60.             //Stop the previous search/Timer before starting it again
  61.             dispatcherTimer.Stop();
  62.             //Start search timer
  63.             dispatcherTimer.Start();
  64.         }
  65.  
  66.         private void dispatcherTimer_Tick(object sender, EventArgs e)
  67.         {
  68.             int counter = 0;
  69.             LstDistro.Items.Clear();
  70.  
  71.             foreach (var eventReturned in returnedResults)
  72.             {
  73.                 userInits er = new userInits();
  74.                 er = eventReturned.Value;
  75.  
  76.                 string LstBackground;
  77.  
  78.                 //Set background to green for every even index in list
  79.                 if ((counter % 2) == 0)
  80.                     LstBackground = "#99FFFFFF";
  81.                 else
  82.                     LstBackground = "#99E8E8E8";
  83.  
  84.                 var listVar = new
  85.                 {
  86.                     LBackground = LstBackground,
  87.                     LInits = er.userInit,
  88.                     LFullName = er.userFullName
  89.                 };
  90.  
  91.                 ComboBoxItem newItem = new ComboBoxItem();
  92.                 newItem.Content = er.userInit;
  93.                 LstDistro.Items.Add(listVar);
  94.                 counter = counter + 1;
  95.             }
  96.  
  97.             var bc = new BrushConverter();
  98.             BtnOk.Background = (Brush)bc.ConvertFrom("#FF333333");
  99.             BtnOk.IsEnabled = true;
  100.             LoadingRing.IsActive = false;
  101.  
  102.             if (mainWindow.LblDistro.Content != "")
  103.             {
  104.                 string searchValue = mainWindow.LblDistro.Content as string;
  105.                 string[] searchArray = searchValue.Split(',');
  106.  
  107.                 foreach (var searchItem in searchArray)
  108.                 {
  109.                     foreach (var item in LstDistro.Items)
  110.                     {
  111.                         if (item.ToString().Contains(searchItem.Remove(searchItem.Length - 1)))
  112.                         {
  113.                             LstDistro.SelectedItems.Add(item);
  114.                             selectionchanged = false;
  115.                         }
  116.                     }
  117.                 }
  118.             }
  119.             dispatcherTimer.Stop();
  120.         }
  121.  
  122.         private void BtnOk_Click(object sender, RoutedEventArgs e)
  123.         {
  124.             if (selectionchanged == true)
  125.             {
  126.                 foreach (var item in LstDistro.SelectedItems)
  127.                 {
  128.                     var curItem = item.ToString();
  129.                     //Trim selected value string to give just the event and subject string
  130.                     int evNameIndex = curItem.IndexOf("LFullName = ");
  131.                     string evNameString = curItem.Substring(evNameIndex + 12);
  132.                     int commaIndex = evNameString.IndexOf('}');
  133.                     evNameString = evNameString.Substring(0, commaIndex);
  134.  
  135.                     //Check to see if a comma should be added before the string addition
  136.                     if (mainWindow.LblDistro.Content == "")
  137.                     {
  138.                         mainWindow.LblDistro.Content = evNameString;
  139.                     }
  140.                     else
  141.                     {
  142.                         //Check to see if Name is already on label. If so dont re add it.
  143.                         if ((mainWindow.LblDistro.Content as string).Contains(evNameString))
  144.                         {
  145.                             //Do nothing
  146.                         }
  147.                         else
  148.                         {
  149.                             mainWindow.LblDistro.Content = mainWindow.LblDistro.Content + " , " + evNameString;
  150.                         }
  151.                     }
  152.                 }
  153.             }
  154.             this.Close();
  155.         }
  156.  
  157.         private void CheckBox_Checked(object sender, RoutedEventArgs e)
  158.         {
  159.             ////Select the Item using the DataContext of the Button
  160.             //object clicked = (e.OriginalSource as FrameworkElement).DataContext;
  161.             //var lbi = LstDistro.ItemContainerGenerator.ContainerFromItem(clicked) as ListBoxItem;
  162.             //lbi.IsSelected = true;
  163.         }
  164.  
  165.         private void LstDistro_SelectionChanged(object sender, SelectionChangedEventArgs e)
  166.         {
  167.            selectionchanged = true;
  168.         }
  169.  
  170.         public class SelectableItem : INotifyPropertyChanged
  171.         {
  172.             private bool _isSelected;
  173.             public bool IsSelected
  174.             {
  175.                 get { return _isSelected; }
  176.                 set
  177.                 {
  178.                     _isSelected = value;
  179.                     OnPropertyChanged("IsSelected");
  180.                 }
  181.             }
  182.  
  183.             public string DisplayName { get; set; }
  184.  
  185.             public event PropertyChangedEventHandler PropertyChanged;
  186.  
  187.             protected virtual void OnPropertyChanged(string propertyName)
  188.             {
  189.                 PropertyChangedEventHandler handler = PropertyChanged;
  190.                 if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
  191.             }
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement