Search code examples
.netsilverlightsilverlight-4.0focustabstop

How to control tabbing order without disabling all focus for controls in this scenario


I got a control that looks like this , it has several text boxes

[1 ][2 ][3 ][4 ]

Now in my app those controls are forming a somekind of a matrix like

[1 ][2 ][3 ][4 ]
[1 ][2 ][3 ][4 ]
[1 ][2 ][3 ][4 ]

Now I wanted to set 1 3 and 4 IsTabStop = false so user can tab through 2nd textboxes only. After I have done that I found out that 1 3 4 now cannot be focused. And to my surprise in WPF this is not true, so its just another Silverlight unexpected limitation!

http://msdn.microsoft.com/ru-ru/library/system.windows.controls.control.istabstop(v=vs.95).aspx

How to proceed with my initial tabbing through [2 ] plan?


Solution

  • I believe you can extend the TextBox control to have it focusable. See this post.

    Attached Property

    public class Ex
    {
        public static bool GetIsTabStop(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsTabStopProperty);
        }
    
        public static void SetIsTabStop(DependencyObject obj, bool value)
        {
            obj.SetValue(IsTabStopProperty, value);
        }
    
        // Using a DependencyProperty as the backing store for IsTabStop.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsTabStopProperty =
            DependencyProperty.RegisterAttached("IsTabStop", typeof(bool), typeof(Ex), new PropertyMetadata(true, Callback));
    
        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as Control;
            if (control != null)
            {
                control.IsTabStop = (bool)e.NewValue;
    
                control.MouseLeftButtonDown += (sender, args) =>
                {
                    if (!control.IsTabStop)
                    {
                        control.IsTabStop = true;
                        control.Focus();
                        control.IsTabStop = false;
                    } 
                };
            }
        }
    }
    

    XAML

        <TextBox HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Margin="215,49,0,0" RenderTransformOrigin="0,7.25"/>
        <TextBox HorizontalAlignment="Left" local:Ex.IsTabStop="False" TextWrapping="Wrap" Text="TextBox" Margin="215,96,0,0" VerticalAlignment="Top"/>
        <RadioButton Content="RadioButton" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="210,144,0,0"/>
        <RadioButton Content="RadioButton" local:Ex.IsTabStop="False" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="213,183,0,0"/>
    

    You can basically attach this to anything that's inherited from Control.