ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [WPF] Set Focus
    C#/WPF 2022. 11. 4. 10:17
    public class EventFocusAttachment
    {
        public static Control GetElementToFocus(Button button)
        {
            return (Control)button.GetValue(ElementToFocusProperty);
        }
    
        public static void SetElementToFocus(Button button, Control value)
        {
            button.SetValue(ElementToFocusProperty, value);
        }
    
        public static readonly DependencyProperty ElementToFocusProperty =
            DependencyProperty.RegisterAttached("ElementToFocus", typeof(Control), 
            typeof(EventFocusAttachment), new UIPropertyMetadata(null, ElementToFocusPropertyChanged));
    
        public static void ElementToFocusPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var button = sender as Button;
            if (button != null)
            {
                button.Click += (s, args) =>
                    {
                        Control control = GetElementToFocus(button);
                        if (control != null)
                        {
                            control.Focus();
                        }
                    };
            }
        }
    }

    728x90

    'C# > WPF' 카테고리의 다른 글

    [WPF] Focus to specific Control  (0) 2022.12.09
    [WPF] Resource Dictionary  (0) 2022.12.02
    [WPF] InputBindings  (0) 2022.11.03
    [WPF] Mutex  (0) 2022.11.03
    [WPF] UI 즉시 업데이트  (1) 2022.09.21

    댓글

Designed by Tistory.