Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Select On Focus
- public static bool GetSelectWhenFocused(DependencyObject obj)
- {
- return (bool)obj.GetValue(SelectWhenFocusedProperty);
- }
- public static void SetSelectWhenFocused(DependencyObject obj, bool value)
- {
- obj.SetValue(SelectWhenFocusedProperty, value);
- }
- // Using a DependencyProperty as the backing store for SelectWhenFocused. This enables animation, styling, binding, etc...
- public static read-only DependencyProperty SelectWhenFocusedProperty =
- DependencyProperty.RegisterAttached("SelectWhenFocused", typeof(bool), typeof(EditableComboBox), new UIPropertyMetadata(OnSelectOnFocusedChanged));
- public static void OnSelectOnFocusedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
- {
- bool SetProperty = (bool)args.NewValue;
- var comboBox = obj as ComboBox;
- if (comboBox == null) return;
- if (SetProperty)
- {
- comboBox.GotFocus += GotFocused;
- Messenger.Default.Register<ComboBox>(comboBox, Focus);
- }
- else
- {
- comboBox.GotFocus -= GotFocused;
- Messenger.Default.Unregister<ComboBox>(comboBox, Focus);
- }
- }
- public static void GotFocused(object sender, RoutedEventArgs e)
- {
- var comboBox = sender as ComboBox;
- if(comboBox == null) return;
- var textBox = comboBox.FindChild(typeof(TextBox), "PART_EditableTextBox") as TextBox;
- if (textBox == null) return;
- textBox.SelectAll();
- }
- public static void Focus(ComboBox comboBox)
- {
- if(comboBox == null) return;
- comboBox.Focus();
- }
- #endregion
- <ComboBox
- IsEditable="True"
- ComboBoxHelper:EditableComboBox.SelectWhenFocused="True"
- x:Name="EditBox" />
- <Button
- Command="{Binding Focus}"
- CommandParameter="{Binding ElementName=EditBox}"
- Grid.Column="1" >Focus</Button>
- public SimpleCommand Focus { get; set; }
- public WindowVM()
- {
- Focus = new SimpleCommand {ExecuteDelegate = x => Broadcast(x as ComboBox)};
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement