Category / Section
How to select entire text in WPF MaskedEdit while getting focused?
1 min read
You can select entire text on WPF MaskedEdit when it got focus by setting the Boolean property to IsSelectAllOnFocus. When the IsSelectAllOnFocus property is True, the whole text of the MaskedEdit control will get select. The same has been demonstrated in the following code example:
XAML:
<Window x:Class="MaskedEdit_Behavior.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace:MaskedEdit_Behavior" xmlns:syncfusion="http://schemas.syncfusion.com/wpf"> <Grid> <StackPanel> <TextBlock Text="MaskedEditExt "/> <local:MaskedEditExt Height="28" Width="180" Margin="0 10 0 0" Text="Test" IsSelectAllOnFocus="true"/> </StackPanel> </Grid> </Window>
C#:
namespace MaskedEdit_Behavior { public class MaskedEditExt:SfMaskedEdit { public bool IsSelectAllOnFocus { get { return (bool)GetValue(IsSelectAllOnFocusProperty); } set { SetValue(IsSelectAllOnFocusProperty, value); } } // Using a DependencyProperty as the backing store for IsSelectAllOnFocus. This enables animation, styling, binding, etc... public static readonly DependencyProperty IsSelectAllOnFocusProperty = DependencyProperty.Register("IsSelectAllOnFocus", typeof(bool), typeof(MaskedEditExt), new PropertyMetadata(false)); protected override void OnGotFocus(System.Windows.RoutedEventArgs e) { base.OnGotFocus(e); if (IsSelectAllOnFocus) SelectAll(); } protected override void OnPreviewMouseDown(System.Windows.Input.MouseButtonEventArgs e) { if (IsSelectAllOnFocus) { if (IsKeyboardFocusWithin) return; e.Handled = true; Focus(); } else base.OnPreviewMouseDown(e); } } }