Created
August 23, 2022 18:44
-
-
Save lafleurh/914b99ac2d9337ee51baf5322d52e5fd to your computer and use it in GitHub Desktop.
WPF Set Focus on Textbox when Clicking on Button as Behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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