0

私はStackPanel複数Expanderの を持っています:

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

そして、これらの動作を実装したいと思います:

  • 1 つまたは両方Expanderを展開できます
  • Expanderアクティブにできるのは 1 つだけです (ヘッダーの背景を変更します)
  • パネル内で別のコントロールを選択するとアクティブExpanderが変更されますが、パネル外の他のコントロールを選択するとアクティブのままになりますExpanderExpanderExpander

どうすればこれを達成できますか?

4

1 に答える 1

1

StackPanelではなくListControlに追加します。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);
            }
        }
    }
}

この投稿に対する私の答えを参照してください。背後にあるコードの機能:リンクテキスト

于 2009-09-29T14:32:50.210 に答える