Skip to content

Instantly share code, notes, and snippets.

@rkmax
Created August 6, 2012 21:48
Show Gist options
  • Save rkmax/3278762 to your computer and use it in GitHub Desktop.
Save rkmax/3278762 to your computer and use it in GitHub Desktop.
Clase de Comportamientos para agregar a los controles
using System.Windows;
using System.Windows.Controls;
namespace Rkmax.Controls
{
public class Behaviors
{
public static readonly DependencyProperty SelectTextOnFocusProperty = DependencyProperty
.RegisterAttached("SelectTextOnFocus", typeof(bool), typeof(Behaviors), new FrameworkPropertyMetadata(false, GotFocus));
public static void SetSelectTextOnFocus(DependencyObject obj, bool value)
{
obj.SetValue(SelectTextOnFocusProperty, value);
}
private static void GotFocus(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var textbox = d as TextBox;
if (null == textbox) return;
textbox.GotKeyboardFocus += SelectTextOnFocus;
textbox.GotMouseCapture += SelectTextOnFocus;
}
private static void SelectTextOnFocus(object sender, RoutedEventArgs e)
{
if (!(sender is TextBox)) return;
((TextBox)sender).SelectAll();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment