Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found a way, but there's got to be something better.</p> <p><br/> Looking at the Expander through Mole, or looking at it's ControlTemplate generated by Blend we can see that the header part that is responding to Space/Enter/Click/etc is really a ToggleButton. Now the bad news, Because the Header's ToggleButton has a diffrent layout for the Expander's Expanded properties Up/Down/Left/Right it's already has styles assigned to it through the Expander's ControlTemplate. That precludes us from doing something simple like creating a default ToggleButton style in the Expander's Resources.</p> <p><a href="http://i44.tinypic.com/2dlq1pl.png" rel="nofollow noreferrer">alt text http://i44.tinypic.com/2dlq1pl.png</a></p> <p>If you have access to the code behind, or don't mind adding a CodeBehind to the Resource Dictionary that the expander is in, then you can access the ToggleButton and set the TabIndex in the Expander.Loaded event, like this:</p> <pre><code>&lt;Expander x:Name="uiExpander" Header="_abc" Loaded="uiExpander_Loaded" TabIndex="20" IsTabStop="False"&gt; &lt;TextBox TabIndex="30"&gt; &lt;/TextBox&gt; &lt;/Expander&gt; </code></pre> <p><br/></p> <pre><code>private void uiExpander_Loaded(object sender, RoutedEventArgs e) { //Gets the HeaderSite part of the default ControlTemplate for an Expander. var header = uiExpander.Template.FindName("HeaderSite", uiExpander) as Control; if (header != null) { header.TabIndex = uiExpander.TabIndex; } } </code></pre> <p>You can also just cast the sender object to an Expander too, if you need it to work with multiple expanders. The other option, is to create your own ControlTemplate for the Expander(s) and set it up in there.</p> <p><strong>EDIT</strong> We can also move the code portion to an AttachedProperty, making it much cleaner and easier to use:</p> <pre><code>&lt;Expander local:ExpanderHelper.HeaderTabIndex="20"&gt; ... &lt;/Expander&gt; </code></pre> <p>And the AttachedProperty:</p> <pre><code>public class ExpanderHelper { public static int GetHeaderTabIndex(DependencyObject obj) { return (int)obj.GetValue(HeaderTabIndexProperty); } public static void SetHeaderTabIndex(DependencyObject obj, int value) { obj.SetValue(HeaderTabIndexProperty, value); } // Using a DependencyProperty as the backing store for HeaderTabIndex. This enables animation, styling, binding, etc... public static readonly DependencyProperty HeaderTabIndexProperty = DependencyProperty.RegisterAttached( "HeaderTabIndex", typeof(int), typeof(ExpanderHelper), new FrameworkPropertyMetadata( int.MaxValue, FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnHeaderTabIndexChanged))); private static void OnHeaderTabIndexChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { var expander = o as Expander; int index; if (expander != null &amp;&amp; int.TryParse(e.NewValue.ToString(), out index)) { if (expander.IsLoaded) { SetTabIndex(expander, (int)e.NewValue); } else { // If the Expander is not yet loaded, then the Header will not be costructed // To avoid getting a null refrence to the HeaderSite control part we // can delay the setting of the HeaderTabIndex untill after the Expander is loaded. expander.Loaded += new RoutedEventHandler((i, j) =&gt; SetTabIndex(expander, (int)e.NewValue)); } } else { throw new InvalidCastException(); } } private static void SetTabIndex(Expander expander, int index) { //Gets the HeaderSite part of the default ControlTemplate for an Expander. var header = expander.Template.FindName("HeaderSite", expander) as Control; if (header != null) { header.TabIndex = index; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload