Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #region Select On Focus
  2.  
  3. public static bool GetSelectWhenFocused(DependencyObject obj)
  4. {
  5. return (bool)obj.GetValue(SelectWhenFocusedProperty);
  6. }
  7.  
  8. public static void SetSelectWhenFocused(DependencyObject obj, bool value)
  9. {
  10. obj.SetValue(SelectWhenFocusedProperty, value);
  11. }
  12.  
  13. // Using a DependencyProperty as the backing store for SelectWhenFocused. This enables animation, styling, binding, etc...
  14. public static read-only DependencyProperty SelectWhenFocusedProperty =
  15. DependencyProperty.RegisterAttached("SelectWhenFocused", typeof(bool), typeof(EditableComboBox), new UIPropertyMetadata(OnSelectOnFocusedChanged));
  16.  
  17. public static void OnSelectOnFocusedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
  18. {
  19. bool SetProperty = (bool)args.NewValue;
  20.  
  21. var comboBox = obj as ComboBox;
  22. if (comboBox == null) return;
  23.  
  24. if (SetProperty)
  25. {
  26. comboBox.GotFocus += GotFocused;
  27. Messenger.Default.Register<ComboBox>(comboBox, Focus);
  28. }
  29. else
  30. {
  31. comboBox.GotFocus -= GotFocused;
  32. Messenger.Default.Unregister<ComboBox>(comboBox, Focus);
  33. }
  34. }
  35.  
  36. public static void GotFocused(object sender, RoutedEventArgs e)
  37. {
  38. var comboBox = sender as ComboBox;
  39. if(comboBox == null) return;
  40.  
  41. var textBox = comboBox.FindChild(typeof(TextBox), "PART_EditableTextBox") as TextBox;
  42. if (textBox == null) return;
  43.  
  44. textBox.SelectAll();
  45. }
  46.  
  47. public static void Focus(ComboBox comboBox)
  48. {
  49. if(comboBox == null) return;
  50. comboBox.Focus();
  51. }
  52. #endregion
  53.  
  54. <ComboBox
  55. IsEditable="True"
  56. ComboBoxHelper:EditableComboBox.SelectWhenFocused="True"
  57. x:Name="EditBox" />
  58.  
  59. <Button
  60. Command="{Binding Focus}"
  61. CommandParameter="{Binding ElementName=EditBox}"
  62. Grid.Column="1" >Focus</Button>
  63.  
  64. public SimpleCommand Focus { get; set; }
  65. public WindowVM()
  66. {
  67. Focus = new SimpleCommand {ExecuteDelegate = x => Broadcast(x as ComboBox)};
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement