2014-10-31 2 views
0

WPF UserControl에 특정 종속성 속성 DepProp이 있습니다.키를 누를 때 WPF 종속성 속성을 수정하십시오.

Shift 또는 Alt을 누를 때이 속성을 수정하고 키를 놓을 때 이전 값으로 돌아가고 싶습니다.

내가 원했던 것은 트리거와 유사하지만 "Shift 키를 누른 상태"와 같은 조건을 설정할 수 있는지 여부는 알 수 없습니다.

키를 눌렀을 때 명령을 실행할 수 있다고 이해하는 한 컨트롤에 KeyBindings을 지정할 수 있지만 키를 놓을 때 이전 vlaue는 복원하지 않는다는 것을 알고 있습니다.

어떻게하는지에 대한 아이디어가 있으십니까?

+0

해당 키를 캡처하려는 범위는 무엇입니까? 부모 창 전체에서, 키보드 포커스가'UserControl' 내에있을 때, 또는 무엇을해야합니까? –

+0

두 번째 경우로 충분합니다. – elnigno

답변

1

트리에서 상속 된 첨부 된 읽기 전용 속성을 유지 관리하는 일부 '범위'요소 (예 : UserControl)에 첨부 할 수있는 첨부 된 동작을 만들 수 있습니다. 그런 다음 첨부 된 속성에 Trigger을 간단하게 추가 할 수 있습니다.

public sealed class AltShiftHotKeyBehavior : Behavior<FrameworkElement> 
{ 
    private const ModifierKeys AltShift = ModifierKeys.Alt | ModifierKeys.Shift; 

    private static readonly DependencyPropertyKey IsAltShiftPressedPropertyKey = 
     DependencyProperty.RegisterAttachedReadOnly(
      "IsAltShiftPressed", 
      typeof(bool), 
      typeof(AltShiftHotKeyBehavior), 
      new FrameworkPropertyMetadata(
       false, 
       FrameworkPropertyMetadataOptions.Inherits)); 

    public static readonly DependencyProperty IsAltShiftPressedProperty = 
     IsAltShiftPressedPropertyKey.DependencyProperty; 

    public static bool GetIsAltShiftPressed(DependencyObject element) 
    { 
     return (bool)element.GetValue(IsAltShiftPressedProperty); 
    } 

    protected override void OnAttached() 
    { 
     base.OnAttached(); 

     var element = this.AssociatedObject; 

     element.AddHandler(
      FrameworkElement.LoadedEvent, 
      (RoutedEventHandler)OnLoaded, 
      handledEventsToo: true); 

     element.AddHandler(
      FrameworkElement.UnloadedEvent, 
      (RoutedEventHandler)OnUnloaded, 
      handledEventsToo: true); 

     element.AddHandler(
      UIElement.PreviewKeyDownEvent, 
      (KeyEventHandler)OnKey, 
      handledEventsToo: true); 

     element.AddHandler(
      UIElement.PreviewKeyUpEvent, 
      (KeyEventHandler)OnKey, 
      handledEventsToo: true); 

     element.AddHandler(
      UIElement.LostKeyboardFocusEvent, 
      (KeyboardFocusChangedEventHandler)OnLostKeyboardFocus, 
      handledEventsToo: true); 

     var window = element as Window; 
     if (window != null) 
     { 
      window.Activated += OnWindowActivated; 
      window.Deactivated += OnWindowDeactivated; 
     } 

     CheckToggledState(); 
    } 

    protected override void OnDetaching() 
    { 
     ClearToggledState(); 

     base.OnDetaching(); 

     var element = this.AssociatedObject; 

     element.RemoveHandler(
      FrameworkElement.LoadedEvent, 
      (RoutedEventHandler)OnLoaded); 

     element.RemoveHandler(
      FrameworkElement.UnloadedEvent, 
      (RoutedEventHandler)OnUnloaded); 

     element.RemoveHandler(
      UIElement.PreviewKeyDownEvent, 
      (KeyEventHandler)OnKey); 

     element.RemoveHandler(
      UIElement.PreviewKeyUpEvent, 
      (KeyEventHandler)OnKey); 

     element.RemoveHandler(
      UIElement.LostKeyboardFocusEvent, 
      (KeyboardFocusChangedEventHandler)OnLostKeyboardFocus); 

     var window = element as Window; 
     if (window != null) 
     { 
      window.Activated -= OnWindowActivated; 
      window.Deactivated -= OnWindowDeactivated; 
     } 
    } 

    private void CheckToggledState() 
    { 
     var element = this.AssociatedObject; 
     if (element.IsLoaded && 
      element.IsKeyboardFocusWithin && 
      Keyboard.PrimaryDevice.Modifiers == AltShift) 
     { 
      element.SetValue(IsAltShiftPressedPropertyKey, true); 
     } 
     else 
     { 
      element.ClearValue(IsAltShiftPressedPropertyKey);     
     } 
    } 

    private void ClearToggledState() 
    { 
     this.AssociatedObject.ClearValue(IsAltShiftPressedPropertyKey); 
    } 

    private void OnLoaded(object sender, RoutedEventArgs e) 
    { 
     CheckToggledState(); 
    } 

    private void OnUnloaded(object sender, RoutedEventArgs e) 
    { 
     ClearToggledState(); 
    } 

    private void OnWindowActivated(object sender, EventArgs e) 
    { 
     CheckToggledState(); 
    } 

    private void OnWindowDeactivated(object sender, EventArgs e) 
    { 
     ClearToggledState(); 
    } 

    private void OnLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) 
    { 
     CheckToggledState(); 
    } 

    private void OnKey(object sender, KeyEventArgs e) 
    { 
     CheckToggledState(); 
    } 
} 
+0

고마워, 곧 이걸 시험해 보겠다. – elnigno

관련 문제