daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5.  
  6. namespace loraderon.Controls
  7. {
  8.     public partial class SelectButton : UserControl
  9.     {
  10.         public event RoutedEventHandler Click;
  11.         public event SelectionChangedEventHandler SelectionChanged;
  12.  
  13.         public static readonly DependencyProperty ItemsSourceProperty =
  14.             DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(SelectButton));
  15.         public static readonly DependencyProperty ItemTemplateProperty =
  16.                 DependencyProperty.Register("ItemTemplate", typeof(DataTemplate), typeof(SelectButton), new UIPropertyMetadata(null));
  17.         public static readonly DependencyProperty IsExpandedProperty =
  18.                 DependencyProperty.Register("IsExpanded", typeof(bool), typeof(SelectButton));
  19.         public static readonly DependencyProperty SelectedIndexProperty =
  20.                 DependencyProperty.Register("SelectedIndex", typeof(int), typeof(SelectButton));
  21.  
  22.         public SelectButton()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         public IEnumerable ItemsSource
  28.         {
  29.             get { return (IEnumerable)GetValue(ItemsSourceProperty); }
  30.             set { SetValue(ItemsSourceProperty, value); }
  31.         }
  32.  
  33.         public DataTemplate ItemTemplate
  34.         {
  35.             get { return (DataTemplate)GetValue(ItemTemplateProperty); }
  36.             set { SetValue(ItemTemplateProperty, value); }
  37.         }
  38.  
  39.         public int SelectedIndex
  40.         {
  41.             get { return (int)GetValue(SelectedIndexProperty); }
  42.             set { SetValue(SelectedIndexProperty, value); }
  43.         }
  44.  
  45.         public bool IsExpanded
  46.         {
  47.             get { return (bool)GetValue(IsExpandedProperty); }
  48.             set
  49.             {
  50.                 SetValue(IsExpandedProperty, value);
  51.                 if (!value)
  52.                     collapsedAt = DateTime.Now;
  53.                 if (value)
  54.                     ListBox.Focus();
  55.             }
  56.         }
  57.         private DateTime collapsedAt = DateTime.MinValue;
  58.  
  59.         private void Expander_Expanded(object sender, RoutedEventArgs e)
  60.         {
  61.             if (DateTime.Now - collapsedAt <= TimeSpan.FromMilliseconds(200))
  62.             {
  63.                 Expander.IsExpanded = false;
  64.                 IsExpanded = false;
  65.                 return;
  66.             }
  67.             IsExpanded = true;
  68.         }
  69.  
  70.         private void Expander_Collapsed(object sender, RoutedEventArgs e)
  71.         {
  72.             IsExpanded = false;
  73.         }
  74.  
  75.         private void Button_Click(object sender, RoutedEventArgs e)
  76.         {
  77.             if (Click != null)
  78.                 Click(this, e);
  79.         }
  80.  
  81.         private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  82.         {
  83.             ButtonContent.Content = ListBox.SelectedItem;
  84.  
  85.             if (SelectionChanged != null)
  86.                 SelectionChanged(this, e);
  87.             IsExpanded = false;
  88.         }
  89.  
  90.         private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
  91.         {
  92.             ButtonContent.Width = SplitGrid.ColumnDefinitions[0].ActualWidth;
  93.         }
  94.     }
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top