Skip to content

Instantly share code, notes, and snippets.

@lafleurh
Created August 23, 2022 18:44
Show Gist options
  • Save lafleurh/914b99ac2d9337ee51baf5322d52e5fd to your computer and use it in GitHub Desktop.
Save lafleurh/914b99ac2d9337ee51baf5322d52e5fd to your computer and use it in GitHub Desktop.
WPF Set Focus on Textbox when Clicking on Button as Behavior
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
namespace myapp.Views
{
public class EditNameBehavior : Behavior<Button>
{
public static readonly DependencyProperty TextBoxProperty =
DependencyProperty.Register("TextBox", typeof(TextBox), typeof(EditNameBehavior));
public TextBox TextBox
{
get { return (TextBox)GetValue(TextBoxProperty); }
set { SetValue(TextBoxProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Click += AssociatedObject_Click;
}
private void AssociatedObject_Click(object sender, System.Windows.RoutedEventArgs e)
{
if (TextBox != null)
{
TextBox.SelectAll();
TextBox.Focus();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment