당신은 첨부 된 비헤이비어를 사용하여이 작업을 수행 할 수 있습니다.
예
public static class TextBoxBehaviour
{
public static bool GetSelectAll(TextBoxBase target)
{
return (bool)target.GetValue(SelectAllAttachedProperty);
}
public static void SetSelectAll(TextBoxBase target, bool value)
{
target.SetValue(SelectAllAttachedProperty, value);
}
public static readonly DependencyProperty SelectAllAttachedProperty = DependencyProperty.RegisterAttached("SelectAll", typeof(bool), typeof(TextBoxBehaviour), new UIPropertyMetadata(false, OnSelectAllAttachedPropertyChanged));
static void OnSelectAllAttachedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
((TextBoxBase)o).SelectAll();
}
}
사용
<Style TargetType="{x:Type TextBox}" xmlns:behaviours="clr-namespace:Controls.Behaviours">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Background" Value="#FFFFD1D9"/>
<Setter Property="behaviours:TextBoxBehaviour.SelectAll" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
참조
주의 : 그냥 작동해야하지만 ™ 이론적으로, 위의 구현을 테스트 할 수 없습니다.
HTH,
+1 우수 답변. 거의 내가 썼을 것입니다. 개인적 속성 잘못 사용하는 경우 단순히 '((TextBoxBase) 타겟) .SelectAll()'하도록 것은 이해할 수있는 예외를 발생하는 데에있어서의 본체를 변경할 것이다. 품질 보증의 관점에서이 자동으로 무시하고 이러한 오류보다 장기적으로 더 나은 : 당신은 그들을 년 동안 숨겨 잠복하는 것보다 앞이 아니라 최대 당신의 버그를 잡을 수있어. –
@ 레이 번즈 : 나는 동의한다. 내가 벗어나려고 노력하고있는 것은 나쁜 습관이다. 나는 나의 대답을 편집했다. – Dennis
감사합니다. 완벽하게 작동했습니다. 참고로하는 PasswordBox의이 구현하고자하는 사람을 위해 : 별도의 PasswordBoxBehaviour 클래스를 구현하고 PasswordBox의와 TextBoxBase의 모든 항목을 대체합니다. – BCA