2009-09-29 10 views
0

J'ai un StackPanel avec plusieurs Expander s:StackPanel avec plusieurs extenseurs

<StackPanel Margin="0,10,0,0"> 
    <Expander Header="Test 1"> 
     <ListBox> 
      <ListBoxItem Content="Unit 1"/> 
      <ListBoxItem Content="Unit 2"/> 
     </ListBox> 
    </Expander> 
    <Expander Header="Test 2"> 
     <ListBox> 
      <ListBoxItem Content="Unit 3"/> 
      <ListBoxItem Content="Unit 4"/> 
     </ListBox> 
    </Expander> 
</StackPanel> 

Et je veux mettre en œuvre ces comportements:

  • un ou les deux Expander (s) pourrait être élargi
  • un seul Expander peut être actif (modifier le fond d'en-tête)
  • l'actif Expander changera si je sélectionne une autre Expander à l'intérieur du panneau si je sélectionne d'autres Expander ou d'autres contrôles en dehors du panneau des Expander actifs séjours

Comment puis-je y parvenir?

Répondre

1

Ajoutez-les à un ListControl au lieu d'un StackPanel. ListControls prend en charge la sélection d'un élément.

XAML:

<Window x:Class="ExpanderTest.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Height="300" Width="300"> 

    <Window.Resources> 

     <Style TargetType="ListBoxItem"> 
      <Style.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red"/> 
       <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Red"/> 
      </Style.Resources> 

     </Style> 

    </Window.Resources> 

    <StackPanel Margin="0,10,0,0"> 
     <ListBox SelectedIndex="1"> 
      <ListBoxItem HorizontalContentAlignment="Stretch"> 
       <Expander Header="Test 1"> 
        <ListBox> 
         <ListBoxItem Content="Unit 1"/> 
         <ListBoxItem Content="Unit 2"/> 
        </ListBox> 
       </Expander> 
      </ListBoxItem> 
      <ListBoxItem HorizontalContentAlignment="Stretch"> 
       <Expander Header="Test 2" > 
        <ListBox> 
         <ListBoxItem Content="Unit 3"/> 
         <ListBoxItem Content="Unit 4"/> 
        </ListBox> 
       </Expander> 
      </ListBoxItem> 
     </ListBox> 
    </StackPanel> 

</Window> 

code derrière:

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media; 

namespace ExpanderTest 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      EventManager.RegisterClassHandler(typeof(UIElement), 
             GotFocusEvent, 
             new RoutedEventHandler(OnGotFocus)); 
     } 

     private static void OnGotFocus(object sender, RoutedEventArgs e) 
     { 
      // Check if element that got focus is contained by a listboxitem and 
      // in that case selected the listboxitem. 

      DependencyObject parent = e.OriginalSource as DependencyObject; 
      while (parent != null) 
      { 
       ListBoxItem clickedOnItem = parent as ListBoxItem; 
       if (clickedOnItem != null) 
       { 
        clickedOnItem.IsSelected = true; 
        return; 
       } 

       parent = VisualTreeHelper.GetParent(parent); 
      } 
     } 
    } 
} 

Voir ma réponse à ce poste ce que le code fait derrière: link text