【问题标题】:Stackpanel with multiple expanders带有多个扩展器的 Stackpanel
【发布时间】:2009-09-29 12:31:04
【问题描述】:

我有一个StackPanel 和多个Expanders:

<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>

我想实现这些行为:

  • 一个或两个Expander(s) 可以展开
  • 只有一个Expander 可以处于活动状态(更改标题背景)
  • 如果我在面板内选择另一个Expander,活动的Expander 将会改变,但是如果我在面板外选择其他Expander 或其他控件,活动的Expander 将保持不变

我怎样才能做到这一点?

【问题讨论】:

    标签: wpf stackpanel expander


    【解决方案1】:

    将它们添加到 ListControl 而不是 StackPanel。 ListControls 支持选择一个项目。

    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>
    

    后面的代码:

    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);
                }
            }
        }
    }
    

    查看我对这篇文章的回答背后的代码是做什么的:link text

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2014-05-13
      • 2015-10-14
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多